0

How to find and replace all URL paths in an HTML file? I have an HTML file with links from Wayback Machine, like these:

"/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"

The 2016*** part is dynamic. How do I extract these elements:

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

I have tried:

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

1 Answer 1

1

Try this regular expression: \/web.*blog\.mydomain\.com(.*):

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

Check it out in action: https://regex101.com/r/m5ZaRo/3

Sign up to request clarification or add additional context in comments.

1 Comment

Warning: preg_replace(): Delimiter must not be alphanumeric or backslash

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.