仅当缺少站点URL时才添加它 [英] add site url only if it is missing

查看:17
本文介绍了仅当缺少站点URL时才添加它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更改缺少网站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);

OR(@匿名者提供的解决方案)

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

这篇关于仅当缺少站点URL时才添加它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆