I want to replace every link I find within a string, with a modified version of the string, say for example:
The quick brown fox jumped over the http://www.google.com/?v=abc and http://www.google.com/?v=x_y-z
I would replace (and modify) the links in this so it becomes: http://www.google.com/v/abc and http://www.google.com/v/x_y-z
I know how to find all the links using preg_match_all($pattern, $text, $out, PREG_SET_ORDER);
and I can manipulate the strings using preg_split etc – This is done one at a time.
The outcome I’m looking for is:
The quick brown fox jumped over the http://www.google.com/v/abc and http://www.google.com/v/=x_y-z
However how could I match and replace all of them? Any help would be greatly appreciated.
Use
preg_replacefor that:This assumes that you want to match everything after
?v=and put it after the/v/. If that’s not the case, you’ll have to be more specific about what the pattern is.