preg_replace_callback替换preg_replace
$tem = $arr['invoice_type'];
echo "
";<br />print_r($tem);<br />输出<br />a:2:{s:4:"type";a:3:{i:0;s:8:"个人发票";i:1;s:8:"企业发票";i:2;s:0:"";}s:4:"rate";a:3:{i:0;d:6;i:1;d:6;i:2;d:0;}}<br /><br />$teem []= preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $arr['invoice_type'] );<br />echo "<pre class="brush:php;toolbar:false">";<br />print_r($teem);<br />输出<br />Array<br />(<br /> [0] => a:2:{'s:'.strlen('type').':"type";'a:3:{i:0;'s:'.strlen('个人发票').':"个人发票";'i:1;'s:'.strlen('企业发票').':"企业发票";'i:2;'s:'.strlen('').':"";'}'s:'.strlen('rate').':"rate";'a:3:{i:0;d:6;i:1;d:6;i:2;d:0;}}<br />)<br /><br />$teemm []= preg_replace_callback('!s:(\d+):"(.*?)";!s', function ($matches) {return strlen('$2').':\"$2\";';}, $arr['invoice_type'] );<br />echo "<pre class="brush:php;toolbar:false">";<br />print_r($teemm);<br />输出<br />Array<br />(<br /> [0] => a:2:{2:\"$2\";a:3:{i:0;2:\"$2\";i:1;2:\"$2\";i:2;2:\"$2\";}2:\"$2\";a:3:{i:0;d:6;i:1;d:6;i:2;d:0;}}<br />)<br /><br />如何让preg_replace_callback输出的信息和preg_replace输出的一样<br><font color='#FF8000'>------解决方案--------------------</font><br><pre class="brush: php">$s = preg_replace_callback('/s:\d+:"(.+?)";/s',<br /> function($r) {<br /> $n = strlen($r[1]);<br /> return "s:$n:\"$r[1]\";";<br /> }, $s);<br />
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号