如何在html文件中替换url路径

时间:2017-02-06 00:52:10

标签: php regex

如何查找和替换HTML文件中的所有URL路径?我有一个带有Wayback Machine链接的HTML文件,如下所示:

"/web/2016***/http://blog.mydomain.com/archive/img.jpg"    
"/web/2016***/http://blog.mydomain.com/archive/img2.jpg"
"/web/2016***/http://blog.mydomain.com/archive/page2.html"

2016***部分是动态的。如何提取这些元素:

"/archive/img.jpg"
"/archive/img2.jpg"
"/archive/page2.html"

我试过了:

$html = $url;
$content = file_get_contents($html);
$newhtml = preg_replace( 'web/-[^-.]*\./' , '/' , $content);
file_put_contents('post1.html', $newhtml);

1 个答案:

答案 0 :(得分:1)

试试这个正则表达式:\/web.*blog\.mydomain\.com(.*)

preg_replace('\/web.*blog\.mydomain\.com(.*)', '\1', $content);

查看实际操作:https://regex101.com/r/m5ZaRo/3

相关问题