Php regular expression replacement examples

Source: Internet
Author: User

Related concepts of PHP regular expression replacement:

Preg_replace: Search and replace regular expressions.

Mixed preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit]) preg_replace: Allows you to replace the regular expression that matches your definition in the string.


A simple annotation removal function:

The code is as follows: Copy code

Preg_replace ('[(/*) +. + (*/)]', '', $ val );


This code can remove multiple comments using/* comments */format in PHP and CSS. The three parameters are the regular expression, the string to be replaced, and the target string to be replaced (here the removal function is required, so it is a blank string-> ''). If you want to match sub-rules, you can use $0 to represent all matches, $1, $2, and so on.

Search for matches in pattern in subject and replace them with replacement. If limit is specified, only limit matches are replaced. If limit is omitted or its value is-1, all matches are replaced.

Replacement can contain reverse references in the form of // n or (starting from PHP 4.0.4) $ n. The latter is preferred. Each such reference will be replaced with the text that matches the child pattern in the nth captured parentheses. N can range from 0 to 99. // 0 or $0 indicates the text that is matched by the entire mode. Count left parentheses from left to right (starting from 1) to obtain the number of child modes.

When the replacement mode is followed by a reverse reference followed by a number (that is, the number next to a matching mode), the familiar // 1 symbol cannot be used to represent the reverse reference. For example,/11 will make preg_replace () confused about whether a reverse reference of // 1 is followed by a number 1 or a reverse reference of // 11. The solution in this example is to use/$ {1} 1. This will form an isolated $1 reverse reference, and make the other 1 just plain text.


Example 1:

The code is as follows: Copy code

$ Jobname = "Executive Office-RH021001 front-end Secretary (job No.: RH021001 )";
$ Jobname = preg_replace ("/(. *)/", "", $ jobname );
$ Jobname = preg_replace ("/. *-. * 01/", "", $ jobname );
// Output: front-end Secretary

Example 2:

The code is as follows: Copy code

$ Str = "<a href =" space. php? Uid = 109 "> fsb </a>"; s <a href = "space. php? Uid = 109 & do = thread & id = 174 "> Adjust your mindset to prevent mental fatigue in the workplace </a> ";
 
$ Str = preg_replace ("/space. php? Uid = (d +) & do = thread & id = (d +)/","/space-0-do-thread-id-0000000022.16.html ", $ str );
$ Str = preg_replace ("/space. php? Uid = (d +)/","/space-0000000012.16.html ", $ str );
 
// Output: <a href = "space. php? Uid = 109 "> fsb </a>"; s <a href = "/space-0-do-thread-id-174.html"> pay attention to adjust mentality to prevent mental fatigue in the workplace </a>


Example 3: Regular expression extraction or replacement of img tag attributes

 

The code is as follows: Copy code

<? Php

/* PHP regular expression extraction any attribute in the image img Mark */
$ Str = '<center> <br/> PHP regular expression extraction or image change img any attribute in the tag </center> ';

// 1. Retrieve the entire image code
Preg_match ('/<s * imgs + [^>] *? Srcs * = s * ('| ")(.*?) \ 1 [^>] *? /? S *>/I ', $ str, $ match );
Echo $ match [0];

// 2. Obtain the width
Preg_match ('//I ', $ str, $ match );
Echo $ match [1];

// 3. Get the height
Preg_match ('//I ', $ str, $ match );
Echo $ match [1];

// 4. Obtain src
Preg_match ('//I ', $ str, $ match );
Echo $ match [1];

/* PHP regular expression to replace any attribute in the img Mark */
// 1. Replace src = "/uploads/images/20100516000.jpg" with src ="/uploads/uc/images/20100516000.jpg ")
Print preg_replace ('/()/I ', "$ {1} uc/images/$ {3}", $ str );
Echo "

// 2. Replace src = "/uploads/images/20100516000.jpg" with src ="/uploads/uc/images/20100516000.jpg" and save the width and height
Print preg_replace ('/(/I ', "$ {1 }$ {2} uc/images/$ {3}>", $ str );

?>

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.