本文目錄一覽:
php匹配url正則表達式
$url = ‘, , , ‘;
$pattern = “|http:\/\/[^,]+?\.ico,?|U”;
preg_match_all($pattern, $url, $matches);
print_r($matches);
輸出結果:
Array
(
[0] = Array
(
[0] =
[1] =
)
)
php如何使用正則表達式匹配url圖片啊
// 抓取網頁
echo “\n\n抓取網頁=======================================\n”;
function getHTTPS($url) {
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt ( $ch, CURLOPT_HEADER, false );
curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_REFERER, $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, TRUE );
$result = curl_exec ( $ch );
curl_close ( $ch );
return $result;
}
$result = getHTTPS ( “” );
$array = array (
‘img’,
‘script’,
‘link’
);
$num = count ( $array );
for($i = 0; $i $num; ++ $i) {
echo $array [$i] . “——————————–\n”;
if (preg_match_all ( “/” . $array [$i] . “[^]*/i”, $result, $m )) {
for($j = 0; $j count ( $m [0] ); $j ++) {
echo $m [0] [$j] . “\n”;
}
}
}
php 正則表達式 url匹配
1,preg_grep(pattern,array);它的返回值是一個新數組,新數組的元素是成功匹配的元素。
2,preg_match(mode,string);它的返回值是一個整數,0或1,0表示匹配不成功,1表示匹配成功,preg_match()將在第一個匹配成功後停止搜索,不再繼續匹配。
3,preg_match_all(模式,字元串,保存匹配結果數組(multidimensional array));在搜索字元串中,所有匹配的模式都提供正則表達式的匹配結果,並以指定的順序將它們輸出到指定的數組。它執行與字元串末尾匹配的全局正則表達式匹配。
4,preg_quote(string);轉義正則表達式字元,preg_quote()需要一個參數字元串,並在每個正則表達式語法中為該字元添加反斜杠。
5,轉換效果如圖所示。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/238245.html