1

I have tried following so far:

<?php

// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

$text = "The text I want to filter is here. It has urls http://www.example.com and http://www.example.org";

// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url)) {

       // make the urls hyper links
       $final = preg_replace($reg_exUrl, "<a href=\"{$url[0]}\">{$url[0]}</a> ", $text);

       echo $final;

} else {
       // if no urls in the text just return the text
       echo $text;
}

The only issue I am facing is that this is replacing both the URL's with the same url(that is the one found first). How do I loop this to replace each url with their own?

0

2 Answers 2

5

Just use a single preg_replace():

$url_regex = '~(http|ftp)s?://[a-z0-9.-]+\.[a-z]{2,3}(/\S*)?~i';

$text = 'The text I want to filter is here. It has urls https://www.example.com and http://www.example.org';

$output = preg_replace($url_regex, '<a href="$0">$0</a>', $text);

echo $output;

In the replace part, you can refer to groups that were matched by using $0, $1 etc... Group 0 is the whole match.

Another example:

$url_regex = '~(?:http|ftp)s?://(?:www\.)?([a-z0-9.-]+\.[a-z]{2,3}(?:/\S*)?)~i';

$text = 'Urls https://www.example.com and http://www.example.org or http://example.org';

$output = preg_replace($url_regex, '<a href="$0">$1</a>', $text);

echo $output;

// Urls <a href="https://www.example.com">example.com</a> and <a href="http://www.example.org">example.org</a> or <a href="http://example.org">example.org</a>

Using a preg_match() doesn't make sense, regex invocations are relatively expensive performance wise.

PS: I also tweaked your regex a bit along the way.

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

6 Comments

That is great. Thanks @HamZa
@RipHunter See edit. There's no need to use preg_match().
Nice Answer. It answers all my doubts.
Just one last doubt @HamZa. If I want to urlencode $0 and then what should I do?
That is what I wanted. Thanks
|
2

try this:

// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

$text = "The text I want to filter is here. It has urls http://www.example.com and http://www.example.org";

// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url)) {

    // make the urls hyper links
    $final = preg_replace($reg_exUrl, '<a href="$0">$0</a>', $text);

    echo $final;

} else {
    // if no urls in the text just return the text
    echo $text;
}

output:

The text I want to filter is here. It has urls <a href="http://www.example.com">http://www.example.com</a> and <a href="http://www.example.org">http://www.example.org</a>

2 Comments

Just the thing I was looking for! Thanks
If I want to urlencode $0 and then what should I do?

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.