仅在缺少站点 url 时添加站点

add site url only if it is missing

我需要更改某些缺少站点的地方的文本 url。

假设我有一个很大的 html 代码,在该代码中有些地方缺少网站 url,但有些地方有。

示例:/blog/images/image.png 需要更改 http://www.domain.com/blog/images/image.png

所以我尝试了以下代码,

$html=preg_replace('%/blog/%','http://www.domain.com/blog/',$html);

但是使用这段代码它也改变了以下几行,

http://www.domain.com/blog/images/image.png -> http://www.domain.com/http://www.domain.com/blog/images/image.png

我应该如何跳过它?

基本上我需要将网站 url(http://www.domain.com/) 添加到某些地方,只有当它丢失时才需要。

有点棘手。只需从 url 中删除 http://www.domain.com/ 并手动将 http://www.domain.com/ 添加到 url.

尝试

$html = "http://www.domain.com/".str_ireplace('http://www.domain.com/','',$html);

找到http://www.domain.com/http://www.domain.com/的值相加两次并替换为http://www.domain.com/

尝试

$html = preg_replace('%/blog/%','http://www.domain.com/blog/',$html);
$html = str_ireplace('http://www.domain.com/http://www.domain.com/','http://www.domain.com/',$html);

或(@anonymous 的解决方案)

$html = preg_replace('@(http://www.domain.com)?/blog@iU', 'http://www.domain.com/blog', $html)