Question

I have an obvious hyperlink which I all want to replace in a text to just normal HTML hyperlinks. So this just works for one hyperlink:

$string = '<u>\\n\\\\*HYPERLINK \\"http://www.youtube.com/watch?v=A0VUsoeT9aM\\"A Youtube Video</u>';
$pattern = '/http[?.:=\\w\\d\\/]*/';
$namePattern = '/(?:")([\\s\\w]*)</';
preg_match($pattern, $string, $matches);
preg_match($namePattern, $string, $nameMatches);

echo '<a href="'.$matches[0].'">'.$nameMatches[1].'</a>';

But there are more hyperlinks than just one in a text so I want to just change all of these hyperlinks:

 <?php 
    $input = 'Blablabla Beginning Text <u>\\n\\\\*HYPERLINK \\"http://www.youtube.com/watch?v=A0VUsoeT9aM\\"1.A Youtube Video</u> blablabla Text Middle <u>\\n\\\\*HYPERLINK \\"http://www.youtube.com/watch?v=A0VUsoeT9aM\\"2. A Youtube Video</u> blabla Text after';
    //To become:
    $output = 'Blablabla Beginning Text <a href="http://www.youtube.com/watch?v=A0VUsoeT9aM">1. A Youtube Video</a>blablabla Text Middle <a href="http://www.youtube.com/watch?v=A0VUsoeT9aM">2. A Youtube Video</a> blabla Text after';
    ?>

How would I do that?

Was it helpful?

Solution

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...).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top