PHP Preg_replace Function Basics and Example code _php tutorial

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

The electronic modifier makes the Preg_replace function () an alternative to therapy, and the appropriate reference as a parameter is replaced by the PHP tutorial code. Tip: Make sure the permutation forms 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 to the problem is an array or a string, otherwise.

If a match is found, the new problem is generated, otherwise the subject will be returned 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);

Example 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 the ksorting model and the alternative, we should get what we want.

Ksort ($patterns);
Ksort ($replacements);
Echo preg_replace ($patterns, $replacements, $string);

Change 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 ("/( ]*>)/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 after 15s

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

$document = curl_exec ($ch);

$search = Array (' @ @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 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 from form and script tags, which are ignored by PHP functions, such as with strip_tags (they strip the only tagged text, leaving the complete text in the middle).

Regular expressions are divided into two stages to avoid deleting single (and also by s) carriage returns, but still removing all blank lines and multiple line breaks or spaces, and the dressing procedure is performed in 2 stages.
*/
?>


http://www.bkjia.com/PHPjc/445452.html www.bkjia.com true http://www.bkjia.com/PHPjc/445452.html techarticle PHP Tutorial Preg_replace function Basics with instance code//preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit =-1 [ , int $count]]) theme for matching search mode ...

  • 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.