PHP preg_replace function problems

Source: Internet
Author: User
PHP preg_replace function problems
$string = "april 15, 2003";$pattern = "/(/w+) (/d+), (/d+)/i";$replacement = "/${1}1,/$3";print preg_replace($pattern, $replacement, $string);/* output   ======april1,2003*/


$ Replacement = "/$ {1} 1,/$3 ";
"/$ {1} 1,/$3" what does this mean?


$patterns = array ("/(19|20)(/d{2})-(/d{1,2})-(/d{1,2})/",                   "/^/s*{(/w+)}/s*=/");$replace = array ("//3///4///1//2", "$//1 =");print preg_replace ($patterns, $replace, "{startdate} = 1999-5-27");


$ Replace = array ("// 3 // 4 /// 1 // 2", "$ // 1 ="); what does this mean?


Reply to discussion (solution)


$ {1} is the first matching, and $3 is the third matching.

$ {1} is the first matching, and $3 is the third matching.


The second code is wrong. how can I output $ startdate = 5/27/1999?

$ {1} is the first matching, and $3 is the third matching.


$ {1} is there an escape \ slash in front of it not the original output?

The first piece of code is wrong and should be written

$string = "april 15, 2003";$pattern = "/(\w+) (\d+), (\d+)/i";$replacement = "\${1}1,\$3";print preg_replace($pattern, $replacement, $string);
Or
$string = "april 15, 2003";$pattern = "/(\w+) (\d+), (\d+)/i";$replacement = '${1}1,$3';print preg_replace($pattern, $replacement, $string);


Similarly, the second paragraph of code should be written.
$patterns = array ("/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/",                   "/^\s*{(\w+)}\s*=/");$replace = array ("\\3/\\4/\\1/\\2", "$\\1 =");print preg_replace ($patterns, $replace, "{startdate} = 1999-5-27");


$ N or \ n are backward referenced marks, indicating the content in the nth parentheses

The first piece of code is wrong and should be written

$string = "april 15, 2003";$pattern = "/(\w+) (\d+), (\d+)/i";$replacement = "\${1}1,\$3";print preg_replace($pattern, $replacement, $string);
Or
$string = "april 15, 2003";$pattern = "/(\w+) (\d+), (\d+)/i";$replacement = '${1}1,$3';print preg_replace($pattern, $replacement, $string);


Similarly, the second paragraph of code should be written.
$patterns = array ("/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/",                   "/^\s*{(\w+)}\s*=/");$replace = array ("\\3/\\4/\\1/\\2", "$\\1 =");print preg_replace ($patterns, $replace, "{startdate} = 1999-5-27");


$ N or \ n are backward referenced marks, indicating the content in the nth parentheses


Thank you. $ {1} does the previous escape slash \ mean the original output?

In double quotes, php tries to interpret $ as the leading variable.
The string "$ {1}" will cause an undefined variable error.
So escape the code so that $ {1} can be handed over to prea_replace for processing.

In double quotes, php tries to interpret $ as the leading variable.
The string "$ {1}" will cause an undefined variable error.
So escape the code so that $ {1} can be handed over to prea_replace for processing.

Thank you.

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.