Yesterday I discovered that all ereg, eregi functions will be removed from php very soon. I am shocked. It will take weeks and months of work to replace all these functions in the scripts.

Now I am looking for a function to make URLs and emails clickable and then to convert clickable links back to plain text URLs and emails.

I was using this function with eregi_replace:

function mak_clickable($string) {
$string = eregi_replace("(([[:alnum:]]+://)|www\.)([^[:space:]]*)([[:alnum:]#?/&=])", "<a href=\"\\1\\3\\4\" target=\"new\">\\1\\3\\4</a>", $string);
$string = eregi_replace("href=\"www","href=\"http://www", $string);
$string = eregi_replace("(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))", "<a href=\"mailto:\\1\" title=\"E-mail \\1\">\\1</a>", $string);
$string = preg_replace("/>(([[:alnum:]]+:\/\/)|www\.)([^[:space:]]{20,30})([^[:space:]]*)([^[:space:]]{10,20})([[:alnum:]#?\/&=])</", ">\\1\\3...\\5\\6<", $string);
return($string);
}

I am looking for a similar function without eregi_replace which does the following:

  1. Converts all URLs and emails into clickable links.
  2. Long links makes shorter between > and </a> to prevent page design destroy.
  3. Converts clickable links back to plain text URLs and emails for text editinging in a form textarea.

I have checked Word Press make_clickable function. It's OK but it does not make long links shorter between > </a> There are also no function how to convert links back to plain text.

    lpa,

    To remove links, use strip_tags() or use HTMLPurifier.

    Note: htmlpurifier has a bit of a learning curve can remove broken / poorly formed tags better than strip_tags. strip_tags will work prefectly if you know all tags are formed correctly.

    If you are doing this against a large body of html and only want to remove the anchor tags, a quick google search turned up this:

    
    $str = ''; // this would be the string containing the tags to be removed
    $str_noanchor = preg_replace('/<a href="([^<]*)">([^<]*)<\/a>/', '', $str);
    echo $str_noanchor;
    

    And to make a link - and shorten it:

    // found @ other forums not sure if im supposed to post links there or not.. niah
    // google the function you should find it.
    function hyperlink($text){
        // match protocol://address/path/
        $text = preg_replace_callback("#(^| |\n)([a-zA-Z]+://)([.]?[a-zA-Z0-9_/-])*#", create_function('$part',
                                '$text_link = (strlen($part[0]) > 19) ? substr($part[0],0,14) . "..." . substr($part[0],-2) : $part[0];
                                return $part[1] . "<a href=\"{$part[0]}\" title=\"Click to visit: {$part[0]}\" target=\"_blank\">{$text_link}</a>";')
                                , $text);
    
    // match http://www.something
    //added |\n so catches new lines as well and not links in the [url=...] tags or in quotes
    $text = preg_replace_callback("#(^| |\n)(www([.]?[a-zA-Z0-9_/-])*)#", create_function('$part',
                            '$text_link = (strlen($part[2]) > 15) ? substr($part[2],0,10) . "..." . substr($part[2],-2) : $part[2];
                            return $part[1] . "<a href=\"http://{$part[2]}\" title=\"Click to visit: http://{$part[2]}\" target=\"_blank\">{$text_link}</a>";')
                            , $text);
    
    // return $text
    return $text;
    }
    
    

    You can use the general idea, instead of preg_replace they are using preg_replace_callback to use substr to limit the length of the string.

    This should point you in the right direction, if the wordpress function works better for creating the links (without shortening it) try adding the callback function (from above)

    Best of luck...

      Something about the second piece of code quoted by big.nerd: it's usually more convenient to define a real function to do the job and have preg_replace_callback() call it instead of using create_function(). The reason is twofold:

      First, unless the body of the created function changes each time create_function() is invoked (which doesn't happen here), it means a new duplicate function is parsed and created every time. This is wasteful.

      Second, there's no way for syntax errors in the body of the created function to be spotted at compile time, because it's not parsed until run time (until then it's just a plain string).

      Meanwhile and maybe three: since PHP 5.3 the need for create_function() has greatly decreased. The hyperlink() function above can be written as

      function hyperlink($text)
      {
      
      $make_link = function($url_index, $elide)
      {
      	return function($part)use($url_index, $elide)
      	{
      		$url = $part[$url_index];
      		$text_link = (strlen($url) > $elide) ? substr($url, 0, $elide - 5) . "..." . substr($url, -2) : $url;
      		return $part[1] . "<a href=\"$url\" title=\"Click to visit: $url\" target=\"_blank\">$text_link</a>";
      	};
      };
      
      $text = preg_replace_callback('#(^| |\n)([a-zA-Z]+://)([.]?[a-zA-Z0-9_/-])*#', $make_link(0, 19), $text);
      $text = preg_replace_callback("#(^| |\n)(www([.]?[a-zA-Z0-9_/-])*)#", $make_link(2, 15), $text);
      return $text;
      }
      

      Note that the syntax highlighting has identified the internal structure of the anonymous function.

        7 days later
        Write a Reply...