Replace preg_replace_callback with preg_replace

Source: Internet
Author: User
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.

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.