Home > Backend Development > PHP Tutorial > preg_replace_callback替换preg_replace

preg_replace_callback替换preg_replace

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-23 13:51:49
Original
1199 people have browsed it

$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输出的一样  <p>  </p>  <br> <h2>回复讨论(解决方案)</h2> <p class="sougouAnswer">  不知道你要干什么  <br> 实例的结果并不对! </p> <p class="sougouAnswer">  第二个输出弄错了是下面的  <br> $teem []= preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $arr['invoice_type'] );  <br> echo "</p><pre class="brush:php;toolbar:false">";  <br> print_r($teem);  <br> 输出  <br> Array  <br> (  <br>     [0] => 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> )  <p class="sougouAnswer">  </p><p class="yy">   不知道你要干什么   <br> 实例的结果并不对!  </p>  <br>  <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: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> )   <p class="sougouAnswer">  <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sycode" name="code">$s = preg_replace_callback('/s:\d+:"(.+?)";/s',		function($r) {			$n = strlen($r[1]);			return "s:$n:\"$r[1]\";";		}, $s);
Copy after login
Copy after login

$s = preg_replace_callback('/s:\d+:"(.+?)";/s',		function($r) {			$n = strlen($r[1]);			return "s:$n:\"$r[1]\";";		}, $s);
Copy after login



大神我按照你的改了输出数据是这样
代码
 $teem = preg_replace_callback('/s:\d+:"(.+?)";/s',
            function($r) {
                $n = strlen($r[1]);
                return "s:$n:\"$r[1]\";";
            }, $arr['invoice_type'] );
        echo "
";  <br>         print_r($teem);  <br> 输出  <br> a:2:{s:4:"type";a:3:{i:0;s:8:"个人发票";i:1;s:8:"企业发票";i:2;s:12:"";}s:4:"rate";a:3:{i:0;d:6;i:1;d:6;i:2;d:0;}}  <br>  <br> 企业发票后边S:0:""   变为了s:12:""  多了12长度,这个怎么来的  <br>  <p class="sougouAnswer">  没考虑值为空的情况  <br> 把 '/s:\d+:"(.+?)";/s'   <br> 改为 '/s:\d+:"(.*?)";/s' </p> <p class="sougouAnswer">  </p><p class="yy">   没考虑值为空的情况   <br> 把 '/s:\d+:"(.+?)";/s'    <br> 改为 '/s:\d+:"(.*?)";/s'  </p>  <br>  <br> 万分感谢 
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template