0

I have a bunch of strings concatenated together into one that contain text and links. I want to find URLs in the string and want to put href to each one (create a link). I am using a regular expression pattern for finding the URLs (links) in the string. Check my example below:

Example :

    <?php

// The Text you want to filter for urls
        $text = "The text you want to filter goes here. http://google.com/abc/pqr
2The text you want to filter goes here. http://google.in/abc/pqr
3The text you want to filter goes here. http://google.org/abc/pqr
4The text you want to filter goes here. http://www.google.de/abc/pqr";

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


// Check if there is a url in the text
        if (preg_match($reg_exUrl, $text, $url)) {
            // make the urls hyper links
            echo preg_replace($reg_exUrl, "<a href='.$url[0].'>" . $url[0] . "</a> ", $text);
        } else {
            // if no urls in the text just return the text
            echo $text . "<br/>";
        }
        ?>

But it is showing following output :

>  The text you want to filter goes here. **http://google.com/abc/pqr** 2The
> text you want to filter goes here. **http://google.com/abc/pqr** 3The text
> you want to filter goes here. **http://google.com/abc/pqr** 4The text you
> want to filter goes here. **http://google.com/abc/pqr**

Whats wrong with this?

1
  • Use preg_replace_callback if you're unversed with the placeholder syntax. Also there are existing "linkify" tools. Commented Feb 9, 2013 at 20:05

1 Answer 1

2

As your regex is delimited with slashes, you need to be very careful when your regular expression contains them. Often, it's easier just to use different characters to delimit your regular expression: PHP doesn't mind what you use.

Try replacing the first and last "/" characters with another character, e.g. "#" and your code will likely work.

You could also simplify your code and do the whole thing in one call to preg_replace as follows:

<?php

$text = 'The text you want to filter goes here. http://google.com/abc/pqr
    2The text you want to filter goes here. http://google.in/abc/pqr
    3The text you want to filter goes here. http://google.org/abc/pqr
    4The text you want to filter goes here. http://www.google.de/abc/pqr';

echo preg_replace('#(http|https|ftp|ftps)\://[a-zA-Z0-9-.]+.[a-zA-Z]{2,3}(/\S*)?#i', '<a href="$0">$0</a>', $text);
Sign up to request clarification or add additional context in comments.

1 Comment

Lots more solutions in the comments at css-tricks.com/snippets/php/find-urls-in-text-make-links The solution in the blog post itself doesn't work as well.

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.