PHP Preg_replace function Base and instance code

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags .url array code echo function html php php tutorial

PHP Tutorial Preg_replace function Basics and Instance code
Preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit =-1 [, int & $count]]) theme for matching search pattern, replace replace
/*
The pattern to search for. It can be a string or an array of strings.

The electronic modifier enables the Preg_replace function () to replace the treated, appropriately referenced as a parameter is the PHP tutorial code to be replaced. Hint: Make sure the permutation constitutes a valid PHP code string, otherwise PHP will complain about parsing errors in the containing Preg_replace function line ().

return value

The Preg_replace function () returns an array if the argument of the problem is an array or a string, otherwise.

If a match is found, a new problem is generated, otherwise the subject will return unchanged or null if an error occurs.
*/

Instance One

$string = ' April 15, 2003 ';
$pattern = '/(w+) (d+), (d+)/I ';
$replacement = ' ${1}1,$3 ';
Echo preg_replace ($pattern, $replacement, $string);//Instance Two

$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);//through ksorting mode and substitution, we should get what we want.

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 (' 3/4/12 ', ' $ = ');
Echo preg_replace ($patterns, $replace, ' {startdate} = 1999-5-27 ');//filter all HTML tags

Preg_replace ("/(</?) (w+) ([^>]*>) E ",
"' 1 '. Strtoupper (' 2 ')." 3 ' ",
$html _body);//filter All script codes

$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 to
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 15s

curl_setopt ($ch, curlopt_useragent, $user _agent);

$document = curl_exec ($ch);

$search = Array (' @<script[^>]*?>.*?</script> @si ',//strip out JavaScript tutorial www.jzread.com
' @<style[^>]*?>.*?</style> @siu ',//strip style tags properly
' @<[/!] *? [^<>]*?> @si ',//strip out HTML tags
' @<! [ss]*?–[tnr]*>@ ',//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 a plain text version of the page. It uses Curl to retrieve web pages, a combination of regular expressions, to remove all unnecessary whitespace. This feature is even stripped of the form and script tags, which are ignored by PHP functions, such as using strip_tags (they strip the only tagged text, leaving the full text in the middle).

Regular expressions are divided into two phases to avoid deleting a single (also matched by s) carriage return, but all blank lines and multiple line breaks or spaces are still deleted, and the trimming operation takes place in 2 stages.
*/
?>

Related Article

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.