成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

15 個實用的 PHP 正則表達式

開發 后端
對于開發人員來說,正則表達式是一個非常有用的功能,它提供了 查找,匹配,替換 句子,單詞,或者其他格式的字符串。這篇文章主要介紹了15個超實用的php正則表達式,需要的朋友可以參考下。在這篇文章里,我已經編寫了15個超有用 的正則表達式,WEB開發人員都應該將它收藏到自己的工具包。

對于開發人員來說,正則表達式是一個非常有用的功能,它提供了 查找,匹配,替換 句子,單詞,或者其他格式的字符串。這篇文章主要介紹了15個超實用的php正則表達式,需要的朋友可以參考下。在這篇文章里,我已經編寫了15個超有用 的正則表達式,WEB開發人員都應該將它收藏到自己的工具包。

[[158267]]

驗證域名檢驗一個字符串是否是個有效域名

  1. $url = "http://komunitasweb.com/"
  2. if (preg_match('/^(http|https|ftp)://([A-Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?/?/i', $url)) { 
  3.   echo "Your url is ok."
  4. else { 
  5.   echo "Wrong url."

從一個字符串中 突出某個單詞

這是一個非常有用的在一個字符串中匹配出某個單詞 并且突出它,非常有效的搜索結果

 

  1. $text = "Sample sentence from KomunitasWeb, regex has become popular in web programming. Now we learn regex. According to wikipedia, Regular expressions (abbreviated as regex or  
  2.  
  3. regexp, with plural forms regexes, regexps, or regexen) are written in a formal language that can be interpreted by a regular expression processor";  
  4. $text = preg_replace("/b(regex)b/i"'<span style="background:#5fc9f6">1</span>', $text);  
  5. echo $text; 

 

突出查詢結果在你的 WordPress 博客里就像剛才我說的,上面的那段代碼可以很方便的搜索出結果,而這里是一個更好的方式去執行搜索在某個WordPress的博客上打開你的文件 search.php ,然后找到 方法 the_title() 然后用下面代碼替換掉它

 

  1. echo $title;  
  2.  
  3. Now, just before the modified line, add this code:  
  4.  
  5. <php  
  6.   $title   = get_the_title();  
  7.   $keys= explode(" ",$s);  
  8.   $title   = preg_replace('/('.implode('|', $keys) .')/iu',  
  9.     '<strong>\0</strong>',  
  10.     $title);  
  11. >  
  12.  
  13. Save the search.php file and open style.css. Append the following line to it:  
  14.  
  15. strong.search-excerpt { background: yellow; }

從HTML文檔中獲得全部圖片

如果你曾經希望去獲得某個網頁上的全部圖片,這段代碼就是你需要的,你可以輕松的建立一個圖片下載機器人

  1. $images = array(); 
  2. preg_match_all('/(img|src)=("|')[^"'>]+/i', $data, $media); 
  3. unset($data); 
  4. $data=preg_replace('/(img|src)("|'|="|=')(.*)/i',"$3",$media[0]); 
  5. foreach($data as $url) 
  6.   $info = pathinfo($url); 
  7.   if (isset($info['extension'])) 
  8.   { 
  9.     if (($info['extension'] == 'jpg') || 
  10.     ($info['extension'] == 'jpeg') || 
  11.     ($info['extension'] == 'gif') || 
  12.     ($info['extension'] == 'png')) 
  13.     array_push($images, $url); 
  14.   } 

刪除重復字母

經常重復輸入字母? 這個表達式正適合.

  1. $text = preg_replace("/s(w+s)1/i""$1", $text); 

刪除重復的標點

功能同上,但只是面對標點,白白重復的逗號

  1. $text = preg_replace("/.+/i"".", $text); 

匹配一個XML或者HTML標簽

這個簡單的函數有兩個參數:***個是你要匹配的標簽,第二個是包含XML或HTML的變量,再強調下,這個真的很強大

 

  1. function get_tag( $tag, $xml ) { 
  2. $tag = preg_quote($tag); 
  3. preg_match_all('{<'.$tag.'[^>]*>(.*?)</'.$tag.'>.'}', 
  4.           $xml, 
  5.           $matches, 
  6.           PREG_PATTERN_ORDER); 
  7.  
  8. return $matches[1]; 

匹配具有屬性值的XML或者HTML標簽

這個功能和上面的非常相似,但是它允許你匹配的標簽內部有屬性值,例如你可以輕松匹配 <div id=”header”>

 

  1. function get_tag( $attr, $value, $xml, $tag=null ) { 
  2. if( is_null($tag) ) 
  3.   $tag = '\w+'
  4. else 
  5.   $tag = preg_quote($tag); 
  6.  
  7. $attr = preg_quote($attr); 
  8. $value = preg_quote($value); 
  9.  
  10. $tag_regex = "/<(".$tag.")[^>]*$attr\s*=\s*"
  11.         "(['\"])$value\\2[^>]*>(.*?)<\/\\1>/" 
  12.  
  13. preg_match_all($tag_regex, 
  14.          $xml, 
  15.          $matches, 
  16.          PREG_PATTERN_ORDER); 
  17.  
  18. return $matches[3]; 

匹配十六進制顏色值

web開發者的另一個有趣的工具,它允許你匹配和驗證十六進制顏色值.

  1. $string = "#555555"
  2. if (preg_match('/^#(?:(?:[a-fd]{3}){1,2})$/i', $string)) { 
  3. echo "example 6 successful."

查找頁面 title

這段代碼方便查找和打印 網頁 <title> 和</title> 之間的內容

 

  1. $fp = fopen("http://www.catswhocode.com/blog","r"); 
  2. while (!feof($fp) ){ 
  3.   $page .= fgets($fp, 4096); 
  4.  
  5. $titre = eregi("<title>(.*)</title>",$page,$regs); 
  6. echo $regs[1]; 
  7. fclose($fp); 

解釋 Apache 日志

大多數網站使用的都是著名的Apache服務器,如果你的網站也是,那么使用PHP正則表達式解析 apache 服務器日志 怎么樣?

 

  1. //Logs: Apache web server 
  2. //Successful hits to HTML files only. Useful for counting the number of page views. 
  3. '^((?#client IP or domain name)S+)s+((?#basic authentication)S+s+S+)s+[((?#date and time)[^]]+)]s+"(?:GET|POST|HEAD) ((?#file)/[^ ?"]+?.html?)??((?#parameters)[^ ?"]+)? HTTP/[0-9.]+"s+(?#status code)200s+((?#bytes transferred)[-0-9]+)s+"((?#referrer)[^"]*)"s+"((?#user agent)[^"]*)"$' 
  4.  
  5. //Logs: Apache web server 
  6. //404 errors only 
  7. '^((?#client IP or domain name)S+)s+((?#basic authentication)S+s+S+)s+[((?#date and time)[^]]+)]s+"(?:GET|POST|HEAD) ((?#file)[^ ?"]+)??((?#parameters)[^ ?"]+)? HTTP/[0-9.]+"s+(?#status code)404s+((?#bytes transferred)[-0-9]+)s+"((?#referrer)[^"]*)"s+"((?#user agent)[^"]*)"$' 

使用智能引號代替雙引號

如果你是一個印刷愛好者,你將喜歡這個允許用智能引號代替雙引號的正則表達式,這個正則被WORDPRESS在其內容上使用

  1. preg_replace('B"b([^"x84x93x94rn]+)b"B''?1?', $text); 

檢驗密碼的復雜度

這個正則表達式將檢測輸入的內容是否包含6個或更多字母,數字,下劃線和連字符. 輸入必須包含至少一個大寫字母,一個小寫字母和一個數字

'A(?=[-_a-zA-Z0-9]*?[A-Z])(?=[-_a-zA-Z0-9]*?[a-z])(?=[-_a-zA-Z0-9]*?[0-9])[-_a-zA-Z0-9]{6,}z'

WordPress: 使用正則獲得帖子上的圖片

我知道很多人是WORDPRESS的使用者,你可能會喜歡并且愿意使用 那些從帖子的內容檢索下來的圖像代碼。使用這個代碼在你的BLOG只需要復制下面代碼到你的某個文件里

 

  1. <php if (have_posts()) : ?> 
  2. <php while (have_posts()) : the_post(); ?> 
  3.  
  4. <php 
  5. $szPostContent = $post->post_content; 
  6. $szSearchPattern = '~<img [^>]* />~'
  7.  
  8. // Run preg_match_all to grab all the images and save the results in $aPics 
  9. preg_match_all( $szSearchPattern, $szPostContent, $aPics ); 
  10.  
  11. // Check to see if we have at least 1 image 
  12. $iNumberOfPics = count($aPics[0]); 
  13.  
  14. if ( $iNumberOfPics > 0 ) { 
  15.    // Now here you would do whatever you need to do with the images 
  16.    // For this example the images are just displayed 
  17.    for ( $i=0; $i < $iNumberOfPics ; $i++ ) { 
  18.      echo $aPics[0][$i]; 
  19.    }; 
  20. }; 
  21.  
  22. endwhile; 
  23. endif; 

自動生成笑臉圖案

被WordPress使用的另一個方法, 這段代碼可使你把圖像自動更換一個笑臉符號

  1. $texte='A text with a smiley '
  2. echo str_replace(':-)','<img src="smileys/souriant.png">',$texte); 

移除圖片的鏈接

 

  1. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  2. <php 
  3.   $str = ' 
  4.     <a href="http://www.jobbole.com/">jobbole</a>其他字符 
  5.     <a href="http://www.sohu.com/">sohu</a> 
  6.     <a href="http://www.sohu.com/"><img src="http://www.fashion-press.net/img/news/3176/mot_06.jpg" /></a> 
  7.     <br>'; 
  8.  
  9.   //echo preg_replace("/(<a.*?>)(<img.*?>)(<\/a>)/", '$2', $str);  
  10.   echo preg_replace("/(<a.*?>)(<img.*?>)(<\/a>)/"'\2', $str);  

以上就是15個超實用的php正則表達式,希望對大家的學習有所幫助。

責任編輯:王雪燕
相關推薦

2023-09-04 15:52:07

2011-05-11 17:40:30

PHP正則表達式

2019-01-11 18:36:54

開發者技能代碼

2024-09-14 09:18:14

Python正則表達式

2018-09-27 15:25:08

正則表達式前端

2009-09-16 10:59:24

PHP正則表達式元字符

2009-09-16 13:14:10

Ereg正則表達式

2009-09-16 14:32:43

PHP正則表達式替換

2009-09-16 13:24:30

PHP正則表達式匹配

2009-09-16 16:01:57

PHP正則表達式正則表達式的應用

2009-09-16 11:17:12

PHP正則表達式定位字

2009-04-22 15:06:16

正則表達式PHP特殊字符

2020-09-04 09:16:04

Python正則表達式虛擬機

2009-09-16 11:08:39

PHP正則表達式特殊字

2009-11-17 10:20:37

PHP正則表達式

2009-09-16 09:58:53

PHP正則表達式函數

2009-09-16 11:27:52

PHP正則表達式正則表達式實現

2009-09-16 10:43:22

PHP正則表達式函數

2009-09-16 13:53:17

PHP正則表達式匹配

2009-09-16 12:41:42

Perl正則表達式
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品久久久久久久久久三级 | 嫩呦国产一区二区三区av | 日韩欧美在线视频 | 日本黄色片免费在线观看 | 日日日日日日bbbbb视频 | a级在线 | 成人精品一区二区 | 欧美视频 亚洲视频 | 国产精品久久久久久久久久99 | 成人影视网 | 日韩中文字幕一区二区 | 日韩精品a在线观看图片 | 精品国产一区二区三区久久久蜜月 | 青青草一区 | 国产精品爱久久久久久久 | 国产成人在线一区二区 | 岛国av免费在线观看 | 亚洲免费精品 | 日韩一级精品视频在线观看 | 国产精品久久久久久久午夜 | 一级毛片视频 | 成人黄色电影在线播放 | 欧美一级黄带 | 四虎影视1304t | 人人做人人澡人人爽欧美 | 日韩一二三区视频 | 国产ts人妖一区二区三区 | 免费一二区 | 国产一区二区三区久久久久久久久 | 亚洲人成人一区二区在线观看 | 国产精品久久久亚洲 | 日本免费一区二区三区视频 | 久久伊人久久 | 拍真实国产伦偷精品 | 午夜天堂精品久久久久 | 日韩在线免费视频 | 在线视频一区二区 | 97人人爱 | 免费视频一区二区 | 国产成人精品福利 | 欧美aaaa视频 |