日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
15個實用的PHP正則表達式

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

潞城網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、APP開發(fā)、自適應網(wǎng)站建設等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)于2013年成立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設就選創(chuàng)新互聯(lián)。

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

 
 
  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.";
  6. }

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

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

 
 
  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. regexp, with plural forms regexes, regexps, or regexen) are written in a formal language that can be interpreted by a regular expression processor"; 
  3. $text = preg_replace("/b(regex)b/i", '1', $text); 
  4. echo $text;

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

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

從HTML文檔中獲得全部圖片

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

 
 
  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. {
  7.   $info = pathinfo($url);
  8.   if (isset($info['extension']))
  9.   {
  10.     if (($info['extension'] == 'jpg') ||
  11.     ($info['extension'] == 'jpeg') ||
  12.     ($info['extension'] == 'gif') ||
  13.     ($info['extension'] == 'png'))
  14.     array_push($images, $url);
  15.   }
  16. }

刪除重復字母

經(jīng)常重復輸入字母? 這個表達式正適合.

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

刪除重復的標點

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

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

匹配一個XML或者HTML標簽

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

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

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

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

 
 
  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. $attr = preg_quote($attr);
  7. $value = preg_quote($value);
  8. $tag_regex = "/<(".$tag.")[^>]*$attr\s*=\s*".
  9.         "(['\"])$value\\2[^>]*>(.*?)<\/\\1>/"
  10. preg_match_all($tag_regex,
  11.          $xml,
  12.          $matches,
  13.          PREG_PATTERN_ORDER);
  14. return $matches[3];
  15. }

匹配十六進制顏色值

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

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

查找頁面 title

這段代碼方便查找和打印 網(wǎng)頁 之間的內容

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

解釋 Apache 日志

大多數(shù)網(wǎng)站使用的都是著名的Apache服務器,如果你的網(wǎng)站也是,那么使用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. //Logs: Apache web server
  5. //404 errors only
  6. '^((?#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個或更多字母,數(shù)字,下劃線和連字符. 輸入必須包含至少一個大寫字母,一個小寫字母和一個數(shù)字

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

自動生成笑臉圖案

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

 
 
  1. $texte='A text with a smiley ';
  2. echo str_replace(':-)','',$texte);

移除圖片的鏈接

 
 
  1.   $str = '
  2.     jobbole其他字符
  3.     sohu
  4.     
  5.     
    ';
  6.   //echo preg_replace("/()()(<\/a>)/", '$2', $str); 
  7.   echo preg_replace("/()()(<\/a>)/", '\2', $str); 
  8. >

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


當前名稱:15個實用的PHP正則表達式
文章起源:http://www.dlmjj.cn/article/cdiopch.html