php:如何解码十六进制和字符串的混合


php: how to decode a mix of hex and string?

我有这个字符串

Willekensmolenstraat122;3500 HASSELT

一旦用HTML打印出来,应该是:"Willekensmolenstraat 122 3500 HASSELT"

如何解码?

我发现的功能

function decodeHexas($source) {
    return preg_replace('/&#([a-f0-9]+);/mei', 'chr(0x''1)', $source);
}

但它没有给出正确的结果

<?php
$input = '&#87;i&#108;&#108;ek&#101;&#110;&#115;&#109;o&#108;e&#110;&#115;t&#114;a&#97;&#116;122;&#51;5&#48;&#48;&nbsp;&#72;A&#83;&#83;EL&#84;';
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 "&nbsp", let's modify it a bit
    $text = preg_replace("/([a-zA-Z]+)('d+);([0-9]+)'&nbsp;('w+)/", "$1 $2 $3 $4", $text);
    return $text;
}
echo decode_entities($input);
// Result: 
// Willekensmolenstraat 122 3500 HASSELT

正如@castis所说,您可以使用html_entity_decode()

<?php
$string = "&#87;i&#108;&#108;ek&#101;&#110;&#115;&#109;o&#108;e&#110;&#115;t&#114;a&#97;&#116;122;&#51;5&#48;&#48;&nbsp;&#72;A&#83;&#83;EL&#84;";
echo html_entity_decode($string);
?>

您可以在以下网址阅读更多信息:

http://php.net/manual/en/function.html-entity-decode.php