How to use function processing for the value $ {1} in the regular expression? How to Write

Source: Internet
Author: User
How can I use the function to process the value $ {1} in the regular expression? How to write it? $ User_pattern & nbsp ;=& nbsp; "/& nbsp ;\@ (. + ?) ([\ S |:] | $ & nbsp;)/"; & nbsp; $ str & nbsp ;=& nbsp; preg _ How do I use the function to process the value $ {1} in the regular expression? How to write it?
$ User_pattern = "/\ @ (. + ?) ([\ S |:] | $ )/";
$ Str = preg_replace ($ user_pattern, '$ {1}', $ str );

How can I transcode the URL function of $ {1} in Chinese? I can't do this using the following code. it's all strings.

$ Str = preg_replace ($ user_pattern, '$ {1}', $ str );
------ Solution --------------------
Use preg_replace_callback,
For example:

Function cb ($ matches ){
Return sprintf ('% 1 $ s', urlencode ($ matches [1]);
}
$ User_pattern = "/\ @ (. + ?) ([\ S
------ Solution --------------------
:]
------ Solution --------------------
$ )/";
$ Str = 'jacky @ yyq.cn ';
$ Str = preg_replace_callback ($ user_pattern, 'cb ', $ str );
Echo $ str;
------ Solution --------------------
For more information, see the preg_replace_callback 5th parameters. if it is set to 1, it is replaced only once.
------ Solution --------------------
This post was finally edited by xuzuning at 09:53:52. it is feasible to edit your original scheme, but some things are missing.
$ User_pattern = "/\ @ (. + ?) ([\ S
------ Solution --------------------
:]
------ Solution --------------------
$)/E ";
$ Str = preg_replace ($ user_pattern, '"$ {1}"', $ str );
 
Compare it with what you wrote.

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.