濮阳杆衣贸易有限公司

主頁 > 知識庫 > 15個超實用的php正則表達式

15個超實用的php正則表達式

熱門標簽:css百度地圖標注位置顯示 地圖標注商戶中心要收錢多少 實用地圖標注app 鄂州人工智能電銷機器人軟件 線上教育ai外呼系統(tǒng) 宿遷智能外呼系統(tǒng)供應(yīng)商 地圖標注字母的軟件 400免費電話去哪申請 菏澤智能ai電銷機器人銷售公司

在這篇文章里,我已經(jīng)編寫了15個超有用的正則表達式,WEB開發(fā)人員都應(yīng)該將它收藏到自己的工具包。

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

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

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

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

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

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

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

$images = array(); 
preg_match_all('/(img|src)=("|')[^"'>]+/i', $data, $media); 
unset($data); 
$data=preg_replace('/(img|src)("|'|="|=')(.*)/i',"$3",$media[0]); 
foreach($data as $url) 
{ 
  $info = pathinfo($url); 
  if (isset($info['extension'])) 
  { 
    if (($info['extension'] == 'jpg') || 
    ($info['extension'] == 'jpeg') || 
    ($info['extension'] == 'gif') || 
    ($info['extension'] == 'png')) 
    array_push($images, $url); 
  } 
} 

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

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

刪除重復(fù)的標點
功能同上,但只是面對標點,白白重復(fù)的逗號

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

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

function get_tag( $tag, $xml ) { 
 $tag = preg_quote($tag); 
 preg_match_all('{'.$tag.'[^>]*>(.*?)/'.$tag.'>.'}', 
          $xml, 
          $matches, 
          PREG_PATTERN_ORDER); 
 
 return $matches[1]; 
} 

匹配具有屬性值的XML或者HTML標簽
這個功能和上面的非常相似,但是它允許你匹配的標簽內(nèi)部有屬性值,例如你可以輕松匹配 div id=”header”>

function get_tag( $attr, $value, $xml, $tag=null ) { 
 if( is_null($tag) ) 
  $tag = '\w+'; 
 else 
  $tag = preg_quote($tag); 
 
 $attr = preg_quote($attr); 
 $value = preg_quote($value); 
 
 $tag_regex = "/(".$tag.")[^>]*$attr\s*=\s*". 
        "(['\"])$value\\2[^>]*>(.*?)\/\\1>/" 
 
 preg_match_all($tag_regex, 
         $xml, 
         $matches, 
         PREG_PATTERN_ORDER); 
 
 return $matches[3]; 
} 

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

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

查找頁面 title
這段代碼方便查找和打印 網(wǎng)頁 title> 和/title> 之間的內(nèi)容

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

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

//Logs: Apache web server 
//Successful hits to HTML files only. Useful for counting the number of page views. 
'^((?#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)[^"]*)"$' 
 
//Logs: Apache web server 
//404 errors only 
'^((?#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在其內(nèi)容上使用

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

檢驗密碼的復(fù)雜度
這個正則表達式將檢測輸入的內(nèi)容是否包含6個或更多字母,數(shù)字,下劃線和連字符. 輸入必須包含至少一個大寫字母,一個小寫字母和一個數(shù)字

復(fù)制代碼 代碼如下:
'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的使用者,你可能會喜歡并且愿意使用 那些從帖子的內(nèi)容檢索下來的圖像代碼。使用這個代碼在你的BLOG只需要復(fù)制下面代碼到你的某個文件里

?php if (have_posts()) : ?> 
?php while (have_posts()) : the_post(); ?> 
 
?php 
$szPostContent = $post->post_content; 
$szSearchPattern = '~img [^>]* />~'; 
 
// Run preg_match_all to grab all the images and save the results in $aPics 
preg_match_all( $szSearchPattern, $szPostContent, $aPics ); 
 
// Check to see if we have at least 1 image 
$iNumberOfPics = count($aPics[0]); 
 
if ( $iNumberOfPics > 0 ) { 
   // Now here you would do whatever you need to do with the images 
   // For this example the images are just displayed 
   for ( $i=0; $i  $iNumberOfPics ; $i++ ) { 
     echo $aPics[0][$i]; 
   }; 
}; 
 
endwhile; 
endif; 
?> 

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

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

移除圖片的鏈接

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

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

您可能感興趣的文章:
  • 小議正則表達式效率 貪婪、非貪婪與回溯
  • PHP正則表達式抓取某個標簽的特定屬性值的方法
  • 非常重要的php正則表達式詳解
  • php郵箱地址正則表達式驗證
  • php用戶注冊信息驗證正則表達式
  • PHP 正則表達式效率 貪婪、非貪婪與回溯分析(推薦)

標簽:梅州 恩施 咸陽 池州 六安 綿陽 鞍山 三亞

巨人網(wǎng)絡(luò)通訊聲明:本文標題《15個超實用的php正則表達式》,本文關(guān)鍵詞  15個,超實,用的,php,正則,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《15個超實用的php正則表達式》相關(guān)的同類信息!
  • 本頁收集關(guān)于15個超實用的php正則表達式的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    安阳县| 西贡区| 望谟县| 麻阳| 肥乡县| 阿拉善右旗| 太康县| 平武县| 北票市| 铜梁县| 虹口区| 收藏| 海伦市| 元谋县| 江孜县| 本溪市| 当阳市| 白水县| 隆回县| 延边| 徐水县| 天津市| 清徐县| 东丰县| 屯留县| 河曲县| 汉中市| 嘉义市| 华亭县| 江永县| 阿拉善左旗| 西安市| 喜德县| 大新县| 南京市| 巫溪县| 克拉玛依市| 嘉兴市| 淅川县| 永泰县| 沂南县|