I have this link:
http://mysite/myfolder/it/my-keywords.html and want to replace /it/ with /es/ (2 letter country codes)
I could explode() with “/” delimiter but would like to understand if preg_replace would be better.
tried:
preg_replace("/\/([a-z]{2})/\/", $link, $country);
EDIT
answer:
preg_replace("/\/[a-z]{2}\//", "/$country/", $link);
preg_replace is like a swiss army knife. preg rather than ereg means it uses perl compatible regular expressions. It matches first param (regex), replaces with second param (string) in third param (string).
Regular expressions are optimized for efficiency by using search tree cutoff techniques etc… so are generally efficient alternative method.
This should do what you want.