如何将包含img URL的URL更改为img其他链接?

时间:2011-12-15 06:54:47

标签: php

什么是正则表达式模式(在PHP中)用链接替换img字符串,其中标签img URL用作链接的锚文本?例如:

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

如何适应它?

1 个答案:

答案 0 :(得分:0)

使用正则表达式is usually a bad idea解析(x)HTML。我提出了以下基于DOM的解决方案:

$html = 'text <img src="http://mywebsite.com/images1.jpg" />' . "\n" 
  . ' text <img src="http://mywebsite.com/images2.jpg" /> ending text';

$domd = new DOMDocument();
libxml_use_internal_errors(true);
$domd->loadHTML($html);
libxml_use_internal_errors(false);

foreach ($domd->getElementsByTagName("img") as $image) {
  $link = $domd->createElement("a");
  $link->setAttribute("href", $image->getAttribute("src"));
  $image->parentNode->replaceChild($link, $image);
  $link->appendChild($image);
}

//this loop is neccesary so there's no doctype, html and
// some other tags added to the output 
$doc = new DOMDocument();
foreach ($domd->documentElement->firstChild->childNodes as $child)
  $doc->appendChild($doc->importNode($child, true));

var_dump($doc->saveHTML());

输出结果为:

<p>text <a href="http://mywebsite.com/images1.jpg"><img src="http://mywebsite.com/images1.jpg"></a>
 text <a href="http://mywebsite.com/images2.jpg"><img src="http://mywebsite.com/images2.jpg"></a> ending text</p>