Preg_replace_callback Replace Preg_replace

Source: Internet
Author: User
$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\ "; '", $arr [' Invoice_type ']);
echo "
";
Print_r ($teem);
Output
Array
(
[0] = = a:2:{' s: '. strlen (' type '). ': ' type '; ' a:3:{i:0; ' S: '. strlen (' personal Invoice '). ': ' Personal invoice '; ' I:1; ' S: '. strlen (' Corporate Invoice '). ': ' Corporate 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\ "; ';}, $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 let the Preg_replace_callback output the same information as the preg_replace output


Reply to discussion (solution)

I don't know what you're doing.
The result of the instance is not right!

The second output is mistaken.
$teem []= preg_replace ('!s: (\d+): "(. *?)";! Se ', ' s: '. strlen (' $ "). ': \ ' $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're doing.
The result of the instance is not right!



The second output is mistaken.
$teem []= preg_replace ('!s: (\d+): "(. *?)";! Se ', ' s: '. strlen (' $ "). ': \ ' $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);



God, I changed the output data as you did.
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 behind s:0: "" changed to S:12: "" more than 12 length, this How to come

The case of empty value is not considered
Put '/s:\d+: ' (. +?) "; S

The case of empty value is not considered
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.