Preg_replace_callback for a more elegant replacement

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.