0

在网上搜索了一段时间后,我发现有很多在线工具可以将符号转换为 html 数字,但反之则不行。

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

例如:

& -> &

然后回到

& -> &

有人知道吗?

4

3 回答 3

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("–"));
€
于 2011-05-23T11:56:55.547 回答
2

滚动你自己的;)

对于 PHP:谷歌搜索发现h​​tmlentitieshtml_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编写一些简单的东西。例如:

HTML解码

[视觉基础]

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();
于 2009-07-27T10:10:55.227 回答
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);
?>

(取自这里,因为我不知道 php!)。当然,这需要修改以将“&”转换为 38,但我将把它作为练习留给了解 php 的人。

于 2009-07-27T10:10:37.083 回答