1
function makeLinksInTheContent($html)
{
    $html= preg_replace("/(^|[\n ])([\w]*?)((ht|f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is", "$1$2<a href=\"$3\" rel=\"nofollow\" >$3</a>", $html);
    $html= preg_replace("/(^|[\n ])([\w]*?)((www|ftp)\.[^ \,\"\t\n\r<]*)/is", "$1$2<a href=\"http://$3\" rel=\"nofollow\" >$3</a>", $html);
    $html= preg_replace("/(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i", "$1<a href=\"mailto:$2@$3\" rel=\"nofollow\">$2@$3</a>", $html);

    return($html);
}

this is my code.

My need is autolinking the url. Using preg_replace to find the url and set link to this url.

for example: "A page contains www.google.com." if i pass this content to makeLinksInTheContent($html), it will return "A page contains www.google.com."

But The following url format is not getting linked.

  1. (http://www.google.com)

  2. www.test.com()and[]&^%$#@!+|!@#$%^&()_+}{:"?><,./;'[]=-09~`.co,in,com.com

  3. http://www.test.com()and[]&^%$#@!+|!@#$%^&()_+}{:"?><,./;'[]=-09~`.co,in,com.com

  4. https://www.test.com()and[]&^%$#@!+|!@#$%^&()_+}{:"?><,./;'[]=-09~`.co,in,com.com

  5. ftp://www.test.com()and[]&^%$#@!+|!@#$%^&()_+}{:"?><,./;'[]=-09~`.co,in,com.com

I think my regular expression have some mistakes. please suggest us.

2
  • I've already answered this question before...? This EXACT question Commented Jul 25, 2013 at 10:13
  • Duplicate of preg_replace to replace string for matching url Commented Jul 25, 2013 at 10:23

2 Answers 2

3

You can use preg_replace_callback in this case. Read more

Function

<?php
  function replace_urls( $text = null ) {
    $regex  = '/((http|ftp|https):\/\/)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?/';
    return preg_replace_callback( $regex, function( $m ) {
      $link = $name = $m[0];
      if ( empty( $m[1] ) ) {
        $link = "http://".$link;
      }
      return '<a href="'.$link.'" target="_blank" rel="nofollow">'.$name.'</a>';
    }, $text );
  }
?>

Usage

<?php
  $text = "http://stackoverflow.com/questions/17854971/preg-replace-to-replace-string-for-matching-url#17855054
  www.google.com
  https://twitter.com/
  http://www.somelinkwithhash.com/post/4454/?foo=bar#foo=bar";

  echo replace_urls( $text );
?>

Output

<a href="http://stackoverflow.com/questions/17854971/preg-replace-to-replace-string-for-matching-url#17855054" target="_blank" rel="nofollow">http://stackoverflow.com/questions/17854971/preg-replace-to-replace-string-for-matching-url#17855054</a>
<a href="http://www.google.com" target="_blank" rel="nofollow">www.google.com</a>
<a href="https://twitter.com/" target="_blank" rel="nofollow">https://twitter.com/</a>
<a href="http://www.somelinkwithhash.com/post/4454/?foo=bar#foo=bar" target="_blank" rel="nofollow">http://www.somelinkwithhash.com/post/4454/?foo=bar#foo=bar</a>
Sign up to request clarification or add additional context in comments.

1 Comment

The following urls not getting linked.
0

My answer to the last time you asked this:

preg_replace to replace string for matching url

I can suggest using my function - just tested with your data and works for me

function parse_links($string,$mailto=true)
{
    $preg_r = "#(((https?|ftp)(://)?)?[a-zA-Z0-9-_.]{1,64}\.[a-zA-Z0-9-_.]{1,128}\.[a-z]{2,4}(\.[a-z]{2,4})?(/([^ ]{1,256}))?/?)#";
    $string = preg_replace($preg_r,"<a href=\"http://$1\">$1</a>",explode(" ",$string));
    if($mailto)
    {
        $preg_r2 = "/([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z_\-\.][-\w]*[0-9a-zA-Z_\-\.]\.)+[a-zA-Z]{2,9})/";
        $string = preg_replace($preg_r2,"<a href=\"mailto:$1\">$1</a>",$string);
    }
    return implode(" ",$string);
}

Just pass false as second parameter if you don't want email links to be created

2 Comments

The following urls not getting linked. (www.google.com), www.test.com()and[]&^%$#@!+|!@#$%^&()_+}{:"?><,./;'[]=-09~.co,in,com.com, https://www.test.com()and[]&^%$#@!+|!@#$%^&()_+}{:"?><,./;'[]=-09~.co,in,com.com
I get three links when I test that string www.google.com www.test.com and https://www.test.com - what else were you hoping to capture??

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.