php tutorial preg_replace function basics and example code
//preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] ) The subject matches the search pattern and replaces
/*
The pattern to search for. It can be a string or an array of strings.
The electron modifier enables the preg_replace() function to replace after treating the appropriate reference as argument to the PHP tutorial code. Tip: Please make sure that the replacement constitutes a valid PHP code string, otherwise PHP will complain about a parsing error in the line containing the preg_replace() function.
Return value
The preg_replace function() returns an array if the argument to this question is an array or a string, otherwise.
If a match is found, a new question will be generated, otherwise the topic will be returned unchanged or null if an error occurs.
*/
//Example 1
$string = 'april 15, 2003';
$pattern = '/(w+) (d+), (d+)/i';
$replacement = '${1}1,$3';
echo preg_replace($pattern, $replacement, $string);
//Example 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);
//With ksorting patterns and substitutions, we should get what we want.
ksort($patterns);
ksort($replacements);
echo preg_replace($patterns, $replacements, $string);
//Replace a few values
$patterns = array ('/(19|20)(d{2})-(d{1,2})-(d{1,2})/',
'/^s*{(w+)}s*=/');
$replace = array ('3/4/12', '$1 =');
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 15scurl_setopt($ch, curlopt_useragent, $user_agent);
$document = curl_exec($ch);
$search = array('@@si', // strip out javascript tutorial www.bkjia.com
'@[^>);
$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 even strips away form and script tags, which are ignored by PHP functions like strip_tags (they strip only the tag text, leaving the full text in the middle).
The regex was split into two stages, to avoid removing single carriage returns (also matched by s) but still remove all blank lines and multiple newlines or spaces, the trimming operation was performed in 2 stages.
*/
?>