2015-03-02 72 views
0

我有这个字符串php:如何解码十六进制和字符串的混合?

Willekensmolenstraat122;3500 HASSELT 

这给一次打印出来的HTML应该是: “Willekensmolenstraat 122 3500 HASSELT”

我怎样才能解码呢?

我发现功能

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

但它没有给出正确的结果

+4

['html_entity_decode()'](http://php.net/html_entity_decode)? – castis 2015-03-02 21:26:32

+0

PHP有许多处理HTML数据的内置函数。您可能永远不需要编写代码来手动操作HTML数据。 – 2015-03-02 22:27:48

回答

0

由于@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

+0

@yarek你有一些进步? – 2015-04-07 20:01:59

1
<?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 
相关问题