1

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?

5
  • 1
    So use preg_match_all() instead? Commented Mar 30, 2014 at 12:17
  • Yea but how to display all these hyperlinks with surrounding text? Commented Mar 30, 2014 at 12:22
  • @MegaCookie overwrite the $string again . Commented Mar 30, 2014 at 12:23
  • You don't need to double escape character classes like \d or \w. Commented Mar 30, 2014 at 12:38
  • @Ancient Geek You mean just foreach found hyperlink replace the next hyperlink? I don't know if this is performance effective but I will try. Commented Mar 30, 2014 at 12:39

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this just works fine for replacing the links, but I want to remove the <u> \n\*HYPERLINK \"</u> part too. What would you recommend to do? And I'm running into a strange situation, if the Url name has an dot in it, it only creates an hyperlink between till that dot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.