Reputation: 2386
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?
Upvotes: 0
Views: 3754
Reputation: 51
You can do it in java using :
import org.apache.commons.lang.StringEscapeUtils
and using the StringEscapeUtils.unescapeHtml(String str) method
eg output :
System.out.println(StringEscapeUtils.unescapeHtml("@"));
@
System.out.println(StringEscapeUtils.unescapeHtml("€"));
-
System.out.println(StringEscapeUtils.unescapeHtml("–"));
€
Upvotes: 5
Reputation: 5899
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 "walk" the <b>dog</b> 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 <Test String>."
Dim writer As New StringWriter
Server.HtmlDecode(EncodedString, writer)
Dim DecodedString As String = writer.ToString()
[C#]
String EncodedString = "This is a <Test String>.";
StringWriter writer = new StringWriter();
Server.HtmlDecode(EncodedString, writer);
String DecodedString = writer.ToString();
Upvotes: 2
Reputation: 72676
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.
Upvotes: 0