0
什麼是正則表達式模式(在PHP中)用鏈接替換img字符串,其中標記img URL用作鏈接的錨文本?例如:如何將包含img網址的網址更改爲img其他鏈接?
function foo($uri) {
$url = parse_url($uri);
$paths = explode('/', $url['path']);
return sprintf("%s://%s/%s", $url['scheme'], 'http://mywebsite.com', end($paths));
}
$str='text <img src="http://example.com/images1.jpg" />text <img src="http://example.com/img/images2.jpg" /> ending text';
$url = '?<=img\s+src\=[\x27\x22])(?<Url>[^\x27\x22]*)(?=[\x27\x22]';
$str_rep = preg_replace($url, foo($url), $str);
echo $str_rep;
變爲:
text <img src="http://mywebsite.com/images1.jpg" />
text <img src="http://mywebsite.com/images2.jpg" /> ending text
如何適應呢?
有你的榜樣有鬼。 – Maerlyn 2011-12-15 07:09:15