找到包含子域的URL并更改为php中的目录

时间:2014-11-09 16:15:39

标签: php regex preg-replace preg-match

php在加载的html中查找包含“.go.co.in”的子域名并更改为目录

假设

@$dom->loadHTML($html);

在$ html中找到所有网址包含“.go.co.in”并更改为

<a href="http://name1.go.co.in to <a href="http://go.co.in/name1
<a href="http://sub.name2.go.co.in to <a href="http://go.co.in/sub.name2
<a href="http://anything.go.co.in to <a href="http://go.co.in/anything

我尝试了很多方法,但没有成功,请帮帮我

1 个答案:

答案 0 :(得分:0)

您可以使用:

$repl = preg_replace('~(https?://)(.+?)\.(go\.co\.in)(\S*)~', '$1$3/$2$4' $html);

RegEx Demo