PREG

xiaoxiao2021-03-06  21

preg_replace

(PHP 3> = 3.0.9, php 4, php 5) preg_replace - Search and replacement of regular expressions

Description Mixed

preg_replace (Mixed Pattern, Mixed Replacement, Mixed Subject [, INT LIMIT])

Search for the matching of the Pattern mode in Subject and replace it to Replacement. If LIMIT is specified, only the LIMIT match is replaced, if the limit is omitted or its value is -1, all match items will be replaced.

Replacement can contain // n or (from PHP 4.0.4) $ n-in-form, the latter is preferred. Each of these references will be replaced with text that matches the submodes in the Nth captured parentheses. N can range from 0 to 99, where // 0 or $ 0 refers to the text that matches the entire mode. The left to right is countless (starting from 1) to acquire the number of child mode from left to right.

When a replacement mode is followed by a digital (ie, immediately after a matching mode), the familiar // 1 symbol cannot be used to represent the reverse reference. For example, // 11 will make preg_replace () unclear that the reverse reference of one // 1 is followed by a number 1 or a reverse reference of one // 11. The solution in this example is to use / $ {1} 1. This forms an isolated $ 1 reverse reference, and another 1 is just a simple text.

Example 1. Reverse reference to the number of usage

If a match is searched, it will return the replaced Subject, otherwise it returns the original Subject.

Each parameter of preg_replace (except for limit) can be an array. If pattern and replacement are arrays, it will be processed in the order of its key name in an array. This is not necessarily the same as the indexed number. If you use an index to identify which ofces will be replaced by which Replacement will be replaced, you should sort the array with ksort () before calling preg_replace ().

Example 2. Using an index array in preg_replace ()

If Subject is an array, you will perform a search for each project in Subject. And replace, and return an array.

If Pattern and Replacement are arrays, preg_replace () will take values ​​from them to search and replace Subject, respectively. If the value in Replacement is less than the Pattern, use an empty string as the remaining replacement value. If patter is an array and Replacement is a string, each value in Pattern is used as a replacement value. In turn, there is no meaning.

/ E The modifier makes preg_replace () regard the Replacement parameter as a PHP code (after the appropriate reverse reference is replaced). Tip: To ensure that Replacement constitutes a legitimate PHP code string, PHP will restrict syntax resolution errors in the report containing the preg_replace ().

Example 3. Replace several values

This example will output:

$ strtDate = 5/27/1999

Example 4. Using / E modisor

] *>) / e", "'//1'.strtupper('//2').'//3'" , $ Html_body);?>

This will make all HTML tags in the input string into uppercase.

Example 5. Convert HTML into text

] *?>. *? ' si", // remove javascript "<[///!] *? [^ <>] *?> 'si ", // Remove HTML"' ([/ r / n]) [/ s] '", // Remove Blank Character"' & (Quot | # 34); 'I ", // Replace HTML Entity "'& (amp | # 38);' I", "'& (LT | # 60);' I", "'& (GT | # 62);' I", "'& (NBSP | # 160); 'I ","' & (ipcl | # 161); 'I ","' & (cent | # 162); 'I ","' & (Pound | # 163); 'i "," '& (Copy | # 169);' I "," '& # (/ D );' E "); // Run $ replace as a PHP code = array (", "," // 1 ", "/" "," & "," <","> ",", Chr (161), chr (162), chr (169), "chr (// 1)"); $ text = preg_replace ($ SEARCH, $ Replace, $ Document);?>

Note: The LIMIT parameter is added after PHP 4.0.1PL2.

See preg_match (), preg_match_all () and preg_split ().

Add a Note

User contributed notes

preg_replace

Doron88 At Gmail Dot COM

23-Mar-2005 07:00

Hello All PHP EXPERTS :), I Have A Little Questions: I Have A String, Want To Highlight All PHP Codes from this string, how shall i do it? That's my code: PHP Code:

'. Highlight_string (' $ 1 ', true).' ', $ string);?> Naaina At Gmail Dot COM

16-mar-2005 02:25

If you want to remove a parameter from an url, use this function:

Jack At JCXP DOT NET

13-mar-2005 06:08

My Perfect Highlighting Function:) // 2 // 3 ", $ text); Return $ text;} ?>

Jay Blackwood

09-mar-2005 09:58

Do not forget that the regular expression, 'pattern', MUST include the slashes on either end If you forget you get a very unhelpful message,! "Warning: Delimiter must not be alphanumeric" .It is really easy to forget these delimeters as THEY Are In Many Ways Part of The Syntax of A Given Perl Command, NOT The Regular Expression.

07-mar-2005 07:58

Here you have very usefull script to convert links to robots friendly, and read parameters in your page I tested this in the site http:. //Www.proanglo.comYou can use links in you page in such form: http: // www .PHP / S1 = 1 / S2 = 2 / s3 = 3orhttp: //www.example.com/file.php/s1=1 ,s2=2, s3=3your script: anca zaharia

04-mar-2005 05:31

SIMPLE FUNCTION TO STRIP MULTIPLE White Spaces in A String: $ SAMPLE = preg_replace ('// s / s /', '', $ sample);

PHP AT IHID Dot Co Dot UK

26-Feb-2005 10:42

In the function: $ xml = preg_replace ("asd", "/ $ 19); if text starts or finished with a number you will have undexpected behaviour.if $ text =" 3ASD "For Instance, The Function Will BE $ XML = preg_replace (" ASD "," / $ 13ASD / $ 2 ", $ XML); Rather Than Trying to Parse $ 1 Like We Want, It Will Try To Use $ 13, Giving a Very Different Result.confused me for.

Jhm at cotren Dot Net

18-Feb-2005 05:04

IT TOOK ME A While to Figure this one out, but here 'e "," Dechex (ORD (' // 1 ')) ", $ text); Print" Encode: $ encoded / n ";?> Encode: 50485020726f636b7321 decoded: PHP Rocks! Spam At Barad-dur dot nl

17-Feb-2005 04:21

When You Want to your Website you might find this useful. (. *?) $ 0 ", $ matches [0]); '), $ buff; return $ ret;}?> Where $ buff is the content of Your Site and $ Query Is The Query Via $ _REQUEST ( IS An Url-Encode Space)

GBaAtard At Iinet Dot Net Dot Au

15-Feb-2005 12:56

On The Topic of Implementing Forum Code ([B] [/ B] to etc), i Found this workd Well ... ', $ body); $ body = preg_replace (' // [// ([biu]) /] / i ',' ', $ body);?> first line replaces [b] [b] [i] [i] [u] [u] with the appropriate HTML Tags (, , ) second one does the same For Closing Tags ... for Urls, I Use ... // 1 // 2 ', $ body);?> and for urls starting with www., i use ... // 1 // 2 ', $ body);> POP All these Lines Into a function That Receives and returns the text you want 'final coded' and affil you go:) Info at atjeff Dot Co dot NZ

07-Feb-2005 07:47

ive never used regex expressions till now and had loads of difficulty trying to convert a [url] link here [/ url] into an href for use with posting messages on a forum, heres what i manage to come up with: $ patterns = array ("//[LINK/] (.* )/ [//link/]/", "//[url/] (.* )/[//url/]/", "// [IMG /] (.* )/ [/Img/]/ "," //[b/] (.* )/ [//b/]/ "," //[u/] (.*? ) / [// u /] / "," // [【【【【】 ("); $ replacements = array (" // 1 ", " // 1 ", "", "< B> // 1 "," // 1 "); $ newtext = preg_replace ($ patterns, $ replacements, $ TEXT); At First It Would Collect All The Tags Into One Link / Bold / WhatVer, Until I Added THE "? i STILL DONT FULLY UNDERSTAND IT ... But It Works:) Pingju At Stud Dot Ntnu Dot No

03-Feb-2005 07:56

In example 5 shown above, it is said:. $ Document should contain an HTML document As I can see, to remove the Javascript segment from $ document, it must be a string, or only Javascript segment within one line of $ document can be REMOVED, IE THHERE IS NOT / N IN BETWEEN