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í)例一,代碼如下:

  1. $string = 'april 15, 2003'
  2. $pattern = '/(w+) (d+), (d+)/i'
  3. $replacement = '${1}1,$3'
  4. echo preg_replace($pattern$replacement$string); 

實(shí)例二,代碼如下:

  1. $string = 'the quick brown fox jumped over the lazy dog.'
  2. $patterns = array(); 
  3. $patterns[0] = '/quick/'
  4. $patterns[1] = '/brown/'
  5. $patterns[2] = '/fox/'
  6. $replacements = array(); 
  7. $replacements[2] = 'bear'
  8. $replacements[1] = 'black'
  9. $replacements[0] = 'slow'
  10. echo preg_replace($patterns$replacements$string);  

通過ksorting模式和替代,我們應(yīng)該得到我們想要的,代碼如下:

  1. ksort($patterns); 
  2. ksort($replacements); 
  3. echo preg_replace($patterns$replacements$string);  

更換幾個(gè)值,代碼如下:

  1. $patterns = array ('/(19|20)(d{2})-(d{1,2})-(d{1,2})/'
  2.                    '/^s*{(w+)}s*=/'); 
  3. $replace = array ('//''$ ='); 
  4. echo preg_replace($patterns$replace'{startdate} = 1999-5-27');  

過濾所有html標(biāo)簽,代碼如下:

  1. preg_replace("/(</?)(w+)([^>]*>)/e",  
  2.              "'\1'.strtoupper('\2').'\3'",  
  3.              $html_body); 

過濾所有script,代碼如下:

  1. $user_agent = "mozilla/4.0 (compatible; msie 5.01; windows nt 5.0)"
  2.  
  3. $ch = curl_init();    // initialize curl handle  
  4. curl_setopt($ch, curlopt_url, $url); // set url to post to  
  5. curl_setopt($ch, curlopt_failonerror, 1);              // fail on errors  
  6. curl_setopt($ch, curlopt_followlocation, 1);    // allow redirects  
  7. curl_setopt($ch, curlopt_returntransfer,1); // return into a variable  
  8. curl_setopt($ch, curlopt_port, 80);            //set the port number  
  9. curl_setopt($ch, curlopt_timeout, 15); // times out after 15s 
  10.  
  11. curl_setopt($ch, curlopt_useragent, $user_agent); 
  12.  
  13. $document = curl_exec($ch); 
  14.  
  15. $search = array('@<script[^>]*?>.*?</script>@si',  // strip out javascript教程 www.111cn.net 
  16. '@<style[^>]*?>.*?</style>@siu',    // strip style tags properly  
  17. '@<[/!]*?[^<>]*?>@si',            // strip out html tags  
  18. '@<![ss]*?–[ ]*>@',         // strip multi-line comments including cdata  
  19. '/s{2,}/'
  20.  
  21. ); 
  22.  
  23. $text = preg_replace($search" ", html_entity_decode($document)); 
  24.  
  25. $pat[0] = "/^s+/";  
  26. $pat[2] = "/s+$/";  
  27. $rep[0] = "";  
  28. $rep[2] = " "
  29.  
  30. $text = preg_replace($pat$rep, trim($text)); 
  31. //開源代碼phpfensi.com 
  32. 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è)。