我有这个字符串
Willekensmolenstraat122;3500 HASSELT
一旦用HTML打印出来,应该是:"Willekensmolenstraat 122 3500 HASSELT"
如何解码?
我发现的功能
function decodeHexas($source) {
return preg_replace('/&#([a-f0-9]+);/mei', 'chr(0x\1)', $source);
}
但它没有给出正确的结果
<?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
正如@castis所说,您可以使用html_entity_decode()
<?php
$string = "Willekensmolenstraat122;3500 HASSELT";
echo html_entity_decode($string);
?>
您可以在以下网址阅读更多信息:
http://php.net/manual/en/function.html-entity-decode.php