首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将html数字转换为符号?

如何将html数字转换为符号?
EN

Stack Overflow用户
提问于 2009-07-27 10:00:24
回答 3查看 3.6K关注 0票数 0

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

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

例如:

代码语言:javascript
复制
& -> &

然后返回到

代码语言:javascript
复制
& -> &

有人知道这件事吗?

EN

回答 3

Stack Overflow用户

发布于 2011-05-23 19:56:56

您可以在java中使用以下命令来完成此操作:

代码语言:javascript
复制
import org.apache.commons.lang.StringEscapeUtils

并使用StringEscapeUtils.unescapeHtml(String str) method

例如输出:

代码语言:javascript
复制
System.out.println(StringEscapeUtils.unescapeHtml("@")); 
@
System.out.println(StringEscapeUtils.unescapeHtml("€"));
-
System.out.println(StringEscapeUtils.unescapeHtml("–"));
€
票数 5
EN

Stack Overflow用户

发布于 2009-07-27 10:10:55

滚动你自己的;)

对于PHP:谷歌搜索发现htmlentitieshtml_entity_decode

代码语言:javascript
复制
<?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

代码语言:javascript
复制
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#

代码语言:javascript
复制
String EncodedString = "This is a &ltTest String&gt.";
StringWriter writer = new StringWriter();
Server.HtmlDecode(EncodedString, writer);
String DecodedString = writer.ToString();
票数 2
EN

Stack Overflow用户

发布于 2009-07-27 10:10:37

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

代码语言:javascript
复制
#!/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

然后使用以下命令调用它:

代码语言:javascript
复制
python myscript.py "&#38;"

这可以很容易地转换为php或其他东西,基于以下条件:

代码语言:javascript
复制
<?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的人作为练习。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1187360

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档