Phppreg_replace function basics and instance code. Php Tutorial preg_replace function basics and instance code preg_replace (mixed $ pattern, mixed $ replacement, mixed $ subject [, int $ limit-1 [, int $ count]) topic: match the search module php Tutorial preg_replace function basics and instance code
// Preg_replace (mixed $ pattern, mixed $ replacement, mixed $ subject [, int $ limit =-1 [, int & $ count]) subject: match search mode, replace
/*
The pattern to be searched. It can be a string or a string array.
After the electronic modifier causes the preg_replace function () to replace the treatment, the appropriate reference as a parameter is replaced by the php Tutorial code. Tip: Make sure that the replacement constitutes a valid php code string. otherwise, php will complain about parsing errors including the preg_replace function line.
Return value
The preg_replace function () returns an array. if the parameter in this issue is an array or a string, otherwise.
If a match is found, a new problem will occur. Otherwise, the returned topic will remain unchanged or null if an error occurs.
*/
// Instance 1
$ String = '0000l 15,200 3 ';
$ Pattern = '/(w +) (d +), (d +)/I ';
$ Replacement = '$ {1} 1, $3 ';
Echo preg_replace ($ pattern, $ replacement, $ string );
// Instance 2
$ String = 'The quick brown fox jumped over the lazy dog .';
$ Patterns = array ();
$ Patterns [0] = '/quick /';
$ Patterns [1] = '/brown /';
$ Patterns [2] = '/fox /';
$ Replacements = array ();
$ Replacements [2] = 'bear ';
$ Replacements [1] = 'black ';
$ Replacements [0] = 'slow ';
Echo preg_replace ($ patterns, $ replacements, $ string );
// We should get what we want through the ksorting mode and substitution.
Ksort ($ patterns );
Ksort ($ replacements );
Echo preg_replace ($ patterns, $ replacements, $ string );
// Replace several values
$ Patterns = array ('/(19 | 20) (d {2})-(d {1, 2})-(d {1, 2 })/',
'/^ S * {(w +)} s * = /');
$ Replace = array ('2014/1/12', '$1 = ');
Echo preg_replace ($ patterns, $ replace, '{startdate} = ');
// Filter all html tags
Preg_replace ("/( ] *>)/E ",
"'1'. strtoupper ('2'). '3 '",
$ Html_body );
// Filter all script code
$ User_agent = "mozilla/4.0 (compatible; msie 5.01; windows nt 5.0 )";
$ Ch = curl_init (); // initialize curl handle
Curl_setopt ($ ch, curlopt_url, $ url); // set url to post
Curl_setopt ($ ch, curlopt_failonerror, 1); // fail on errors
Curl_setopt ($ ch, curlopt_followlocation, 1); // allow redirects
Curl_setopt ($ ch, curlopt_returntransfer, 1); // return into a variable
Curl_setopt ($ ch, curlopt_port, 80); // set the port number
Curl_setopt ($ ch, curlopt_timeout, 15); // times out after 15 s
Curl_setopt ($ ch, curlopt_useragent, $ user_agent );
$ Document = curl_exec ($ ch );
$ Search = array ('@ ] *?>. *? Script @ Si', // strip out javascript tutorial www.bkjia.com
'@ ] *?>. *? @ Siu ', // strip style tags properly
'@ <[/!] *? [^ <>] *?> @ Si', // strip out html tags
'@ @ ', // Strip multi-line comments including cdata
'/S {2 ,}/',
);
$ Text = preg_replace ($ search, "n", html_entity_decode ($ document ));
$ Pat [0] = "/^ s + /";
$ Pat [2] = "/s + $ /";
$ Rep [0] = "";
$ Rep [2] = "";
$ Text = preg_replace ($ pat, $ rep, trim ($ text ));
Return $ text;
}
/*
This function accepts a url and returns the plain text version of the page. It uses curl to retrieve web pages, a combination of regular expressions, to remove all unnecessary spaces. This function even gets rid of form and script markup, which is ignored by php functions, such as using strip_tags (they strip the unique markup text and leave the complete text in the middle ).
A regular expression is divided into two phases to avoid deleting a single (also matching by s) and pressing enter, but still deleting all blank rows and multiple linefeeds or spaces, the repair procedure was performed in two phases.
*/
?>
Evaluate preg_replace function basics and instance code // preg_replace (mixed $ pattern, mixed $ replacement, mixed $ subject [, int $ limit =-1 [, int $ count]) the topic is the matching search model...