php:如何解码十六进制和字符串的混合?
问题描述:
我有这个字符串php:如何解码十六进制和字符串的混合?
Willekensmolenstraat122;3500 HASSELT
这给一次打印出来的HTML应该是: “Willekensmolenstraat 122 3500 HASSELT”
我怎样才能解码呢?
我发现功能
function decodeHexas($source) {
return preg_replace('/&#([a-f0-9]+);/mei', 'chr(0x\\1)', $source);
}
但它没有给出正确的结果
答
由于@castis说,你可以使用html_entity_decode()
<?php
$string = "Willekensmolenstraat122;3500 HASSELT";
echo html_entity_decode($string);
?>
你可以读到更多在:
+0
@yarek你有一些进步? – 2015-04-07 20:01:59
答
<?php
$input = 'Willekensmolenstraat122;3500 HASSELT';
function decode_entities($text) {
// decode decimal notation (html_entity_decode() will work, too)
$text = preg_replace('/&#(\d+);/me',"chr(\\1)", $text);
// the string contains ";" and " ", let's modify it a bit
$text = preg_replace("/([a-zA-Z]+)(\d+);([0-9]+)\ (\w+)/", "$1 $2 $3 $4", $text);
return $text;
}
echo decode_entities($input);
// Result:
// Willekensmolenstraat 122 3500 HASSELT
['html_entity_decode()'](http://php.net/html_entity_decode)? – castis 2015-03-02 21:26:32
PHP有许多处理HTML数据的内置函数。您可能永远不需要编写代码来手动操作HTML数据。 – 2015-03-02 22:27:48