user2349080
user2349080

Reputation: 37

preg_replace to replace string for matching url

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.

Upvotes: 1

Views: 5184

Answers (2)

bystwn22
bystwn22

Reputation: 1794

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>

Upvotes: 3

MDEV
MDEV

Reputation: 10838

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

Upvotes: 0

Related Questions