在网上搜索了一段时间后,我发现有很多在线工具可以将符号转换为html数字,但反之亦然。
我正在寻找工具/在线工具/php脚本从html数字转换回符号
例如:
& -> &然后返回到
& -> &有人知道这件事吗?
发布于 2011-05-23 19:56:56
您可以在java中使用以下命令来完成此操作:
import org.apache.commons.lang.StringEscapeUtils并使用StringEscapeUtils.unescapeHtml(String str) method
例如输出:
System.out.println(StringEscapeUtils.unescapeHtml("@"));
@
System.out.println(StringEscapeUtils.unescapeHtml("€"));
-
System.out.println(StringEscapeUtils.unescapeHtml("–"));
€发布于 2009-07-27 10:10:55
滚动你自己的;)
对于PHP:谷歌搜索发现htmlentities和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
?>对于.NET,您可以使用HTMLEncode或HTMLDecode编写一些简单的代码。例如:
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();发布于 2009-07-27 10:10:37
这些数字中的大多数都是我相信的ASCII或Unicode值,所以您所需要做的就是查找与该值相关联的符号。对于非unicode元件,这可以像(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然后使用以下命令调用它:
python myscript.py "&"这可以很容易地转换为php或其他东西,基于以下条件:
<?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);
?>(摘自here,因为我不知道php!)当然,这将需要修改以将"&“转换为38,但我将把它留给了解php的人作为练习。
https://stackoverflow.com/questions/1187360
复制相似问题