Preg_replace_callback replace preg_replace $ tem = $ arr ['Invoice _ type'];
Echo"
";
Print_r ($ tem );
Output
A: 2: {s: 4: "type"; a: 3: {I: 0; s: 8: "personal invoice"; I: 1; s: 8: "Enterprise invoice"; I: 2; s: 0: "";} s: 4: "rate"; a: 3: {I: 0; d: 6; I: 1; d: 6; I: 2; d: 0 ;}}
$ Teem [] = preg_replace ('! S :( \ d + ):"(.*?) ";! Se ', "s:'. strlen ('$ 2').': \" $2 \ "; '", $ arr ['Invoice _ type']);
Echo"";
Print_r ($ teem );
Output
Array
(
[0] => a: 2: {'s :'. strlen ('type '). ': "type"; 'A: 3: {I: 0;'s :'. strlen ('Invoice '). ': "personal invoice";' I: 1;'s :'. strlen ('Enterprise invoiced '). ': "Enterprise invoice";' I: 2;'s :'. strlen (''). ': "";'}'s :'. strlen ('rate '). ': "rate"; 'A: 3: {I: 0; d: 6; I: 1; d: 6; I: 2; d: 0 ;}}
)
$ Teemm [] = preg_replace_callback ('! S :( \ d + ):"(.*?) ";! S ', function ($ matches) {return strlen (' $2 '). ': \ "$2 \";';}, $ arr ['Invoice _ type']);
Echo"";
Print_r ($ teemm );
Output
Array
(
[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 ;}}
)
How to make the output information of preg_replace_callback the same as that of preg_replace
Reply to discussion (solution)I don't know what you want to do.
The instance results are not correct!
The second output is incorrect.
$ Teem [] = preg_replace ('! S :( \ d + ):"(.*?) ";! Se ', "s:'. strlen ('$ 2').': \" $2 \ "; '", $ arr ['Invoice _ type']);
Echo"
";
Print_r ($ teem );
Output
Array
(
[0] => a: 2: {s: 4: "type"; a: 3: {I: 0; s: 8: "personal invoice"; I: 1; s: 8: "Enterprise invoice"; I: 2; s: 0: "";} s: 4: "rate"; a: 3: {I: 0; d: 6; I: 1; d: 6; I: 2; d: 0 ;}}
)
I don't know what you want to do.
The instance results are not correct!
The second output is incorrect.
$ Teem [] = preg_replace ('! S :( \ d + ):"(.*?) ";! Se ', "s:'. strlen ('$ 2').': \" $2 \ "; '", $ arr ['Invoice _ type']);
Echo"";
Print_r ($ teem );
Output
Array
(
[0] => a: 2: {s: 4: "type"; a: 3: {I: 0; s: 8: "personal invoice"; I: 1; s: 8: "Enterprise invoice"; I: 2; s: 0: "";} s: 4: "rate"; a: 3: {I: 0; d: 6; I: 1; d: 6; I: 2; d: 0 ;}}
)
$s = preg_replace_callback('/s:\d+:"(.+?)";/s',function($r) {$n = strlen($r[1]);return "s:$n:\"$r[1]\";";}, $s);
$s = preg_replace_callback('/s:\d+:"(.+?)";/s',function($r) {$n = strlen($r[1]);return "s:$n:\"$r[1]\";";}, $s);
I changed the output data according to yours.
Code
$ Teem = preg_replace_callback ('/s: \ d +: "(. + ?) ";/S ',
Function ($ r ){
$ N = strlen ($ r [1]);
Return "s: $ n: \" $ r [1] \ ";";
}, $ Arr ['Invoice _ type']);
Echo"";
Print_r ($ teem );
Output
A: 2: {s: 4: "type"; a: 3: {I: 0; s: 8: "personal invoice"; I: 1; s: 8: "Enterprise invoice"; I: 2; s: 12: "";} s: 4: "rate"; a: 3: {I: 0; d: 6; I: 1; d: 6; I: 2; d: 0 ;}}
Enterprise invoice after S: 0: "" changed to s: 12: "" more than 12 length, how come this?
If the value is null
Put '/s: \ d +: "(. + ?) ";/S'
Change to '/s: \ d + :"(.*?) ";/S'
If the value is null
Put '/s: \ d +: "(. + ?) ";/S'
Change to '/s: \ d + :"(.*?) ";/S'
Thank you very much.