php正則表達式url
A. php如何使用正則表達式匹配url圖片啊
可以這樣:
$image="http://xxxxxxxxx.jpg"
preg_match("/(http://)?w+.jpg/",$image,$matches);//http://可要可不要
echo$matches[0];//$matches[0]即為匹配的圖片路徑
以上只是匹配jpg類型的圖片
如果要匹配其他類型可以這樣使用
preg_match("/(http://)?w+.(jpg|jpeg|gif|png)/",$image,$matches);
echo$matches[0];
B. php 正則表達式 提取指定超鏈接中的url
preg_match_all('/<a[^>]+href="([^"]+)"[^>]+class="green"
[^>]+/Ui', $str, $arr);
print_r($arr[1]);
C. PHP正則表達式之URL截取
$url=$_SERVER["REQUEST_URI"];
$url=parse_url($url);
$url=$url[path];
$url= str_replace("/","",$url)."<br>";
echo str_replace(".php","",$url);
呵呵,辦法是人想出來的嘛,自己想一下啊。。想要什麼就能要到什麼
D. PHP 正則表達式如何替換URL參數
用正則是比較笨的辦法,但也給你提供一下了:
function getpage(){
//你可以把獲取當前page的代碼放在函數里
return 123;
}
$str = 'index.php?main_page=index&cPath=55&pagesize=48';
$ptn = '/&pagesize=(\d+)/';
$pagenum = getpage();
$rep = '&pagesize='.$pagenum;
echo $str; // 輸出:index.php?main_page=index&cPath=55&pagesize=48
preg_replace($ptn,$rep,$str);
echo $str; // 輸出:index.php?main_page=index&cPath=55&pagesize=123
另外多說一下,這種情況可以使用
http_build_query()
這個函數。
具體使用方法:
$u['main_page']=$_GET['main_page'];
$u['cPath']=$_GET['cPath'];
$u['pagesize']=getpage();
$url = 'index.php?'.http_build_query($u);
echo $url;
這個函數很好用,比你自己去匹配好。
E. PHP判斷變數是否為正確的url的正則表達式怎麼寫
if (!preg_match('/http:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is',$變數)){
echo '網頁錯誤';
}把$變數替換成你的變數就可以了
F. php正則表達式如何獲取url裡面的後面id數字
private function getQuerystr($url,$key){
$res = '';
$a = strpos($url,'?');
if($a!==false){
$str = substr($url,$a+1);
$arr = explode('&',$str);
foreach($arr as $k=>$v){
$tmp = explode('=',$v);
if(!empty($tmp[0]) && !empty($tmp[1])){
$barr[$tmp[0]] = $tmp[1];
}
}
}
if(!empty($barr[$key])){
$res = $barr[$key];
}
return $res;
}
getQuerystr($url,'id');
G. PHP 正則表達式如何添加URL參數 ,並把&替換成amp;
preg_replace('/(&|\\?)pagesize=[^&]+/', '', $_SERVER['REQUEST_URI']) str_replace 或replace 或preg_replace 用正則是比較笨的辦法
H. php 正則表達式 url匹配
1,preg_grep(pattern,array);它的返回值是一個新數組,新數組的元素是成功匹配的元素。