Question:
I am working on a multilingual wordpress website where I need to append a loclale in all internal url in a web page.So I have all of the content of the webpage in the $content variable. now
$arr
has only those urls which needs to be appended with a locale but my str_replace is appending all urls that we have in matches[2] array.Answer:
When you replacewww.example.com
with www.example.com/us
, it will replace it anywhere it appears, even if there’s already /us
after it.You can use a regular expression with a negative lookahead to replace a string only if it’s not followed by some other pattern.
/us/
, and will replace it with $new_url
.If you have better answer, please add a comment about this, thank you!