So, you want to replace the found matches, then use preg_replace()
which does exactly that. However, you'll run into one obvious problem: Currently there are two instances of preg_match()
- should those be replaced by two instances of preg_replace()
? No. Combine them.
$pattern = '/http[?.:=\w\d\\/]*/';
$namePattern = '/(?:")([\s\w]*)</';
Can be combined to (I added .
to the $namePattern
part, so it can work with the second example text where the link description contains a dot):
$replacePattern = '/(http[?.:=\w\d\\/]*)\\\\"([\s\w.]*)</';
Because link and text are separated by \\"
in the original text. I tested via preg_match_all()
if this pattern works and it does. Also by adding ()
to the first pattern, they are now grouped.
$replacePattern = '/(http[?.:=\w\d\\/]*)\\\\"([\s\w.]*)</';
// ^-group1-----------^ ^-group2-^
These groupes can now be used in the replace statement.
$replaceWith = '<a href="\\1">\\2</a><';
Where \\1
points to the first group and \\2
to the second. The <
at the end is necessary because preg_replace()
will replace the whole found pattern (not just groups) and since the <
is at the end of the pattern, we would lose it if it wasn't in the replace part.
All that you now need, is to call preg_replace()
with this parameters like the following:
$output = preg_replace($replacePattern, $replaceWith, $string);
All occurences of the $replacePattern
will now be replaced with their version of $replaceWith
and saved in the variable $output
.
You can see it here.
If you want a larger part to be removed, just extend the $replacePattern
.
$replacePattern = '/<u>.*?(http[?.:=\w\d\\/]*)\\\\"([\s\w.]+)<\/u>/';
$replaceWith = '<a href="\\1">\\2</a>';
(see it here) .*?
will match everything and is not greedy, meaning it will stop once it finds the first occurence of whatever comes after (so here it is http...
).