Home  >  Article  >  Backend Development  >  这种地址有什么名称吗

这种地址有什么名称吗

WBOY
WBOYOriginal
2016-06-23 14:23:501404browse

$u1="http://share.feedsportal.com/share/gplus/?u=http%3A%2F%2Fwww.cnbeta.com%2Farticles%2F244192.htm&t=%5B%E5%9B%BE%5D1366+x+768%E5%88%86%E8%BE%A8%E7%8E%87%2B%E8%A7%A6%E6%8E%A7+%E8%81%94%E6%83%B3ThinkPad+X240s%E8%B6%85%E6%9E%81%E6%9C%AC%E6%9B%9D%E5%85%89";$u2="http://t1.baidu.com/it/u=http%3A%2F%2Fauto.shm.com.cn%2Fattachement%2Fjpg%2Fsite1%2F20131008%2F001e90ad746b13bdae620a.jpg&fm=30


如何用正则提出真正的地址
$u1=http://www.cnbeta.com/articles/244192.htm
$u2=http://auto.shm.com.cn/attachement/jpg/site1/20131008/001e90ad746b13bdae620a.jpg


回复讨论(解决方案)

不用正则,化为数组,然后自己组合。

$u1="http://share.feedsportal.com/share/gplus/?u=http%3A%2F%2Fwww.cnbeta.com%2Farticles%2F244192.htm&t=%5B%E5%9B%BE%5D1366+x+768%E5%88%86%E8%BE%A8%E7%8E%87%2B%E8%A7%A6%E6%8E%A7+%E8%81%94%E6%83%B3ThinkPad+X240s%E8%B6%85%E6%9E%81%E6%9C%AC%E6%9B%9D%E5%85%89";$c1 = parse_url($u1, PHP_URL_QUERY);parse_str($c1, $params);echo $params['u'];

$u1="http://share.feedsportal.com/share/gplus/?u=http%3A%2F%2Fwww.cnbeta.com%2Farticles%2F244192.htm&t=%5B%E5%9B%BE%5D1366+x+768%E5%88%86%E8%BE%A8%E7%8E%87%2B%E8%A7%A6%E6%8E%A7+%E8%81%94%E6%83%B3ThinkPad+X240s%E8%B6%85%E6%9E%81%E6%9C%AC%E6%9B%9D%E5%85%89";$c1 = parse_url($u1, PHP_URL_QUERY);parse_str($c1, $params);echo $params['u'];


我需要的是用正则提取

何必呢,2# 的方法其实很好,很高效。

$u1="http://share.feedsportal.com/share/gplus/?u=http%3A%2F%2Fwww.cnbeta.com%2Farticles%2F244192.htm&t=%5B%E5%9B%BE%5D1366+x+768%E5%88%86%E8%BE%A8%E7%8E%87%2B%E8%A7%A6%E6%8E%A7+%E8%81%94%E6%83%B3ThinkPad+X240s%E8%B6%85%E6%9E%81%E6%9C%AC%E6%9B%9D%E5%85%89";$u2="http://t1.baidu.com/it/u=http%3A%2F%2Fauto.shm.com.cn%2Fattachement%2Fjpg%2Fsite1%2F20131008%2F001e90ad746b13bdae620a.jpg&fm=30";preg_match('/u=([^&]+)&/',$u1,$m1);preg_match('/u=([^&]+)&/',$u2,$m2);echo urldecode($m1[1]);echo "
";echo urldecode($m2[1]);

$u1 = "http://share.feedsportal.com/share/gplus/?u=http%3A%2F%2Fwww.cnbeta.com%2Farticles%2F244192.htm&t=%5B%E5%9B%BE%5D1366+x+768%E5%88%86%E8%BE%A8%E7%8E%87%2B%E8%A7%A6%E6%8E%A7+%E8%81%94%E6%83%B3ThinkPad+X240s%E8%B6%85%E6%9E%81%E6%9C%AC%E6%9B%9D%E5%85%89";$u2 = "http://t1.baidu.com/it/u=http%3A%2F%2Fauto.shm.com.cn%2Fattachement%2Fjpg%2Fsite1%2F20131008%2F001e90ad746b13bdae620a.jpg&fm=30";preg_match('/u=([^&]+)/', $u1, $r);echo urldecode($r[1]);preg_match('/u=([^&]+)/', $u2, $r);echo urldecode($r[1]);
http://www.cnbeta.com/articles/244192.htm
http://auto.shm.com.cn/attachement/jpg/site1/20131008/001e90ad746b13bdae620a.jpg

$u1 = "http://share.feedsportal.com/share/gplus/?u=http%3A%2F%2Fwww.cnbeta.com%2Farticles%2F244192.htm&t=%5B%E5%9B%BE%5D1366+x+768%E5%88%86%E8%BE%A8%E7%8E%87%2B%E8%A7%A6%E6%8E%A7+%E8%81%94%E6%83%B3ThinkPad+X240s%E8%B6%85%E6%9E%81%E6%9C%AC%E6%9B%9D%E5%85%89";$u2 = "http://t1.baidu.com/it/u=http%3A%2F%2Fauto.shm.com.cn%2Fattachement%2Fjpg%2Fsite1%2F20131008%2F001e90ad746b13bdae620a.jpg&fm=30";preg_match('/u=([^&]+)/', $u1, $r);echo urldecode($r[1]);preg_match('/u=([^&]+)/', $u2, $r);echo urldecode($r[1]);
http://www.cnbeta.com/articles/244192.htm
http://auto.shm.com.cn/attachement/jpg/site1/20131008/001e90ad746b13bdae620a.jpg
如果不是u=哪就不好使了,能不能从第二个http开始匹配

何必呢,2# 的方法其实很好,很高效。

$u1="http://share.feedsportal.com/share/gplus/?u=http%3A%2F%2Fwww.cnbeta.com%2Farticles%2F244192.htm&t=%5B%E5%9B%BE%5D1366+x+768%E5%88%86%E8%BE%A8%E7%8E%87%2B%E8%A7%A6%E6%8E%A7+%E8%81%94%E6%83%B3ThinkPad+X240s%E8%B6%85%E6%9E%81%E6%9C%AC%E6%9B%9D%E5%85%89";$u2="http://t1.baidu.com/it/u=http%3A%2F%2Fauto.shm.com.cn%2Fattachement%2Fjpg%2Fsite1%2F20131008%2F001e90ad746b13bdae620a.jpg&fm=30";preg_match('/u=([^&]+)&/',$u1,$m1);preg_match('/u=([^&]+)&/',$u2,$m2);echo urldecode($m1[1]);echo "
";echo urldecode($m2[1]);

我的内容中有中英文或加图片混合的字符串,不是光有上面的url
例如:
<html><body><img  src="http://t1.baidu.com/it/u=http%3A%2F%2Fsrc.house.sina.com.cn%2Fimp%2Fimp%2Fdeal%2Fb7%2F0e%2Fa%2F74e1fd81449bb857c66dc6c2812_p1_mk1_wm35.jpg&fm=30" alt="这种地址有什么名称吗" ></img>新浪乐居讯(记者 李晓娜) 金九月,烟台楼市供需两旺,完美收官。现销售排行榜已出炉,克而瑞数据显示,九月烟台六区商品房成交总金额25.88亿元,环比上涨14.5%。六区项目成交套数排行榜中,开发区华明星海湾 (论坛相册户型样板间点评地图搜索)成交320套,列位第一。万达以2.63亿的成交金额一举拿下</body></html>

那你把 u 去掉不就行了?

那你把 u 去掉不就行了?
谢谢你的回复。

那你把 u 去掉不就行了?

	public static function getRealUrl($url){		$uri=$url;		preg_match_all("/=([^&]+)&/", $url, $match);		if(is_array($match[1]) && sizeof($match[1])>=1){			$uri=$match[1][0];		}		return urldecode($uri);	}


上面的方法在别的地方也有调用,下面的是输出描述
	public function coverDescriptionOut(){		//2013070302		global $str;		$desc=$str->apoDecodeCharacter($this->getDescription());		//2013100802		//处理描述的图片地址		preg_match_all('/<img .+src=[\'"]([^\'"]+)[\'"].* alt="这种地址有什么名称吗" >/siU', $desc, $match);		return (!empty($match[1]))?str_replace($match[1][0], haloURI::getRealUrl($match[1][0]), $desc):$desc;	}


想问一下:上面的coverDescriptionOut方法怎么用preg_replace_callback方法重写一下

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn