Regular expression-PHPforeacharrayreturn regular expression

Source: Internet
Author: User
How does {code...} automatically match the corresponding content in $ a3 with $ a2? My mind was full of paste, so I couldn't think of it. I tried foreach for a long time and didn't get it out. I don't know that the passing PHP could provide a great solution ......
$a1 = '/\{[A-Z]*\}/';$a2 = array('{DOMEA} = 'AAAAAA';'{DOMEB} = 'BBBBBBB';)$a3 = return('    title    {DOMEA}        {DOMEB}');$b1 = preg_replace($a1,$a2,$a3);

How can we achieve $ a2 automatic matching of the corresponding content in $ a3?
My mind was full of paste, so I couldn't think of it. I tried foreach for a long time and didn't get it out. I don't know that the passing PHP could provide a great solution ......

Reply content:
$a1 = '/\{[A-Z]*\}/';$a2 = array('{DOMEA} = 'AAAAAA';'{DOMEB} = 'BBBBBBB';)$a3 = return('    title    {DOMEA}        {DOMEB}');$b1 = preg_replace($a1,$a2,$a3);

How can we achieve $ a2 automatic matching of the corresponding content in $ a3?
My mind was full of paste, so I couldn't think of it. I tried foreach for a long time and didn't get it out. I don't know that the passing PHP could provide a great solution ......


  'AAAAAA','{DOMEB}'=>'BBBBBBB');$a3 = '    title    {DOMEA}        {DOMEB}';echo preg_replace_callback($a1, function($matches) use($a2) {    return $a2[$matches[0]];}, $a3);?>

Http://3v4l.org/KudfG

This is not a regular expression, but it is the most commonstr_replaceNo ......

php$a2 = array('{DOMEA}' => 'AAAAAA','{DOMEB}' => 'BBBBBBB',);$a3 = 'title    {DOMEA}        {DOMEB}';echo str_replace(array_keys($a2), array_values($a2), $a3);

php
$a2 = array('{DOMEA}' => 'AAAAAA','{DOMEB}' => 'BBBBBBB',);$a3 = 'title {DOMEA} {DOMEB}';echo strtr($a3,$a2);

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.