I am using the following preg_replace() call to replace links with a new structure:
preg_replace('/example.com\/sub-dir\/(.*)">/', 'newsite.co.uk/sub-dir/$1.html">', $links);
It almost works, except it doesn’t add on the “.html” to the end of the replace, so it ends up as something like “newsite.co.uk/sub-dir/page-identifier” rather than “newsite.co.uk/sub-dir/page-identifier.html”.
I am sure it’s something simple that I am missing, however Googling the problem hasn’t come up with any useful results so I am hoping someone here can help!
Thanks in advance!
Edit: as an example, here is a link side of $links
<a href="http://example.com/sub-dir/the-identifier">Anchor</a>
The above example works if I changed the regext to (.*?) however the below doesn’t:
<a class="prodCatThumb" title="View product" href="http://example.com/sub-dir/product-identifier">
It ends up as
<a class="prodCatThumb.html" title="View product" href="http://example.com/sub-dir/product-identifier">
Any ideas?
Yes, it’s about
.*Just add
?to that like so:.*?example:
edit:
@Ashley: sure, the question mark makes the preceding token in the regular expression optional. E.g.: colou?r matches both colour and color (from this link).
But, additionaly when you use it with ? it’s the ungready method (this may help explaining: Regular expression with .*? (dot-star-questionmark) matches too much? or this: http://www.phpro.org/tutorials/Introduction-to-PHP-Regex.html)
So, to answer your qq:
the output for this link:
<a class="prodCatThumb" title="View product" href="example.com/sub-dir/product-identifier">would now be:
<a class="prodCatThumb" title="View product" href="newsite.co.uk/sub-dir/product-identifier.html">