我在我的網站上有以下代碼。它用於在一個沒有http://或/前面的html塊中查找圖像。如果是這種情況,它會將網址添加到圖片源的前面。PHP和正則表達式:將網站網址添加到圖像
例如:
<img src="http://domain.com/image.jpg"> will stay the same
<img src="/image.jpg"> will stay the same
<img src="image.jpg"> will be changed to <img src="http://domain.com/image.jpg">
我覺得我的代碼確實是低效的...我如何可以把它用更少的代碼運行的任何想法?
preg_match_all('/<img[\s]+[^>]*src\s*=\s*[\"\']?([^\'\" >]+)[\'\" >]/i', $content_text, $matches);
if (isset($matches[1])) {
foreach($matches[1] AS $link) {
if (!preg_match("/^(https?|ftp)\:\/\//sie", $link) && !preg_match("/^\//sie", $link)) {
$full_link = get_option('siteurl') . '/' . $link;
$content_text = str_replace($link, $full_link, $content_text);
}
}
}
哦男人。我從來不知道這個標籤。感謝您發佈對它的引用。 – 2009-11-30 08:22:29