views:

512

answers:

2

After searching internet for a while, I found that there are a lot of online tool that allow conversion from symbol to html number, but not for vice versa.

I am looking for tool/online tool/php script to convert from html number back to symbol

eg:

& -> &

then back to

& -> &

Does anyone know of this?

A: 

Most of these numbers are just ASCII or unicode values I believe, so all you need to do is look up the symbol associated with that value. For non-unicode symbols, this could be as simple as (python script):

#!/usr/bin/python
import sys

# Iterate through all command line arguments
for entity in sys.argv:
    # Extract just the digits from the string (discard the '&#' and the ';')
    value = "".join([i for i in entity if i in "0123456789"])
    # Get the character with that value
    result = chr(value)
    # Print the result
    print result

Then call it with:

python myscript.py "&"

This could presumably be translated to php or something else very easily, something based on:

<?php
$str = "The string ends in ampersand: ";
$str .= chr(38); /* add an ampersand character at the end of $str */

/* Often this is more useful */

$str = sprintf("The string ends in ampersand: %c", 38);
?>

(taken from here as I don't know php!). Of course, this will need modifying to convert "&" into 38, but I'll leave that as an exercise for someone who knows php.

Al
A: 

Roll your own ;)

For PHP: A google search found htmlentities and html_entity_decode:

<?php
$orig = "I'll \"walk\" the <b>dog</b> now";

$a = htmlentities($orig);

$b = html_entity_decode($a);

echo $a; // I'll &quot;walk&quot; the &lt;b&gt;dog&lt;/b&gt; now

echo $b; // I'll "walk" the <b>dog</b> now


// For users prior to PHP 4.3.0 you may do this:
function unhtmlentities($string)
{
    // replace numeric entities
    $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
    $string = preg_replace('~&#([0-9]+);~e', 'chr("\\1")', $string);
    // replace literal entities
    $trans_tbl = get_html_translation_table(HTML_ENTITIES);
    $trans_tbl = array_flip($trans_tbl);
    return strtr($string, $trans_tbl);
}

$c = unhtmlentities($a);

echo $c; // I'll "walk" the <b>dog</b> now

?>

For .NET You could write something as simple that uses HTMLEncode or HTMLDecode. For example:

HTMLDecode

[Visual Basic]

Dim EncodedString As String = "This is a &ltTest String&gt."
Dim writer As New StringWriter
Server.HtmlDecode(EncodedString, writer)
Dim DecodedString As String = writer.ToString()

[C#]

String EncodedString = "This is a &ltTest String&gt.";
StringWriter writer = new StringWriter();
Server.HtmlDecode(EncodedString, writer);
String DecodedString = writer.ToString();
David Christiansen