The following two methods are used to convert similar & #54620, such fragments into normal text
The first method is to use the regular to find the fragments that need to be replaced, then loop, the second method uses Preg_replace_callback, the second method has less code and looks more elegant.
Method One
<?php
$test _str= "Zeng & #54620, Li & #44397;& #50612;";
Echo unescape ($test _str);
function Unescape ($STR) {
Preg_match_all ("/(?:%u.{4}) |& #x. {4};|&#\d+;|.+/u", $str, $r);
$ar = $r [0];
foreach ($ar as $k = = $v) {
if (substr ($v, 0,2) = = "%u") {
$ar [$k] = Iconv ("Ucs-2be", "UTF-8", Pack ("H4", substr ($v,-4)));
}
ElseIf (substr ($v, 0,3) = = "& #x") {
$ar [$k] = Iconv ("Ucs-2be", "UTF-8", Pack ("H4", substr ($v, 3,-1)));
}
ElseIf (substr ($v, 0,2) = = "the") {
$ar [$k] = Iconv ("Ucs-2be", "UTF-8", Pack ("n", substr ($v, 2,-1)));
}
}
return join ("", $ar);
}
?>
Method Two
<?php
$test _str= "Zeng & #54620, Li & #44397;& #50612;";
echo Preg_replace_callback ("/(%u.{4}) |& #x. {4};|&#\d+;/i", "unescape", $test _str);
function unescape ($str _org) {
$str _org= $str _org[0];
if (substr ($str _org,0,2) = = "%u") {
$str _fin = Iconv ("Ucs-2be", "UTF-8", Pack ("H4", substr ($str _org,-4)));
}
ElseIf (substr ($str _org,0,3) = = "& #x") {
$str _fin = Iconv ("Ucs-2be", "UTF-8", Pack ("H4", substr ($str _org,3,-1)));
}
ElseIf (substr ($str _org,0,2) = = "the") {
$str _fin = Iconv ("Ucs-2be", "UTF-8", Pack ("n", substr ($str _org,2,-1)));
}
return $STR _fin;
}
?>
Preg_replace_callback for a more elegant replacement