preg_replace(mixed $pattern,mixed $replacement,mixed $subject [, int $limit = -1 [, int &$count ]])主題為匹配搜索模式,替換要搜索的模式,它可以是一個(gè)字符串或一個(gè)字符串?dāng)?shù)組.
電子修飾符使preg_replace函數(shù)()替代后,適當(dāng)引用作為參數(shù)是php代碼進(jìn)行替換,提示:請確保置換構(gòu)成一個(gè)有效的php代碼字符串,否則php將在包含preg_replace函數(shù)線()解析錯(cuò)誤.
返回值:preg_replace函數(shù)()返回一個(gè)數(shù)組,如果這個(gè)問題的參數(shù)是一個(gè)數(shù)組或一個(gè)字符串,否則,如果找到匹配,新問題會(huì)產(chǎn)生,否則將返回主題不變或null如果發(fā)生錯(cuò)誤.
實(shí)例一,代碼如下:
- $string = 'april 15, 2003';
- $pattern = '/(w+) (d+), (d+)/i';
- $replacement = '${1}1,$3';
- echo preg_replace($pattern, $replacement, $string);
實(shí)例二,代碼如下:
- $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);
通過ksorting模式和替代,我們應(yīng)該得到我們想要的,代碼如下:
- ksort($patterns);
- ksort($replacements);
- echo preg_replace($patterns, $replacements, $string);
更換幾個(gè)值,代碼如下:
- $patterns = array ('/(19|20)(d{2})-(d{1,2})-(d{1,2})/',
- '/^s*{(w+)}s*=/');
- $replace = array ('//', '$ =');
- echo preg_replace($patterns, $replace, '{startdate} = 1999-5-27');
過濾所有html標(biāo)簽,代碼如下:
- preg_replace("/(</?)(w+)([^>]*>)/e",
- "'\1'.strtoupper('\2').'\3'",
- $html_body);
過濾所有script,代碼如下:
- $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('@<script[^>]*?>.*?</script>@si', // strip out javascript教程 www.111cn.net
- '@<style[^>]*?>.*?</style>@siu', // strip style tags properly
- '@<[/!]*?[^<>]*?>@si', // strip out html tags
- '@<![ss]*?–[ ]*>@', // strip multi-line comments including cdata
- '/s{2,}/',
- );
- $text = preg_replace($search, " ", html_entity_decode($document));
- $pat[0] = "/^s+/";
- $pat[2] = "/s+$/";
- $rep[0] = "";
- $rep[2] = " ";
- $text = preg_replace($pat, $rep, trim($text));
- //開源代碼phpfensi.com
- return $text;
- }
此函數(shù)接受一個(gè)url并返回頁面的純文本版本,它使用curl來檢索網(wǎng)頁,一個(gè)正則表達(dá)式的組合,以去除所有不必要的空白,這個(gè)功能甚至剝奪了從形式和script標(biāo)記,這是由php函數(shù)忽略,如用strip_tags,他們地帶唯一的標(biāo)記文本,留下完整的文字在中間.
正則表達(dá)式被分為兩個(gè)階段,以避免刪除單,也由 s的匹配,回車,但仍然刪除所有空白行和多個(gè)換行符或空格,修整手術(shù)進(jìn)行了2個(gè)階段進(jìn)行.
轉(zhuǎn)載請注明來源:php preg_replace函數(shù)基礎(chǔ)與實(shí)例代碼哈爾濱品用軟件有限公司致力于為哈爾濱的中小企業(yè)制作大氣、美觀的優(yōu)秀網(wǎng)站,并且能夠搭建符合百度排名規(guī)范的網(wǎng)站基底,使您的網(wǎng)站無需額外費(fèi)用,即可穩(wěn)步提升排名至首頁。歡迎體驗(yàn)最佳的哈爾濱網(wǎng)站建設(shè)。
