如何将html编号转换为符号?

时间:2009-07-27 10:00:24

标签: html html-entities

在搜索互联网一段时间后,我发现有很多在线工具允许从符号转换为HTML编号,但反之亦然。

我正在寻找工具/在线工具/ php脚本将html编号转换回符号

例如:

& -> &

然后回到

& -> &

有人知道吗?

3 个答案:

答案 0 :(得分:5)

你可以在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("–"));
€

答案 1 :(得分:2)

滚动你自己;)

对于PHP:Google搜索找到了htmlentitieshtml_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

?>

对于.NET您可以编写一些使用HTMLEncodeHTMLDecode的简单内容。例如:

<强> 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();

答案 2 :(得分:0)

我相信这些数字中的大多数只是ASCII或unicode值,因此您需要做的就是查找与该值相关联的符号。对于非unicode符号,这可以像(python脚本)一样简单:

#!/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 "&#38;"

这可能很容易被翻译成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!)。当然,这需要修改以转换“&amp;”进入38,但我会把它留给那些知道php的人练习。