add site url only if it is missing(仅当缺少站点URL时才添加它)
本文介绍了仅当缺少站点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时才添加它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:仅当缺少站点URL时才添加它


基础教程推荐
猜你喜欢
- Libpuzzle 索引数百万张图片? 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01