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);它的返回值是一个新数组,新数组的元素是成功匹配的元素。