0

How to modify this function so it could add target="_blank" attribute to external links? Take the default domain as example.com

function makeLinks($text){
 if(eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\\1">\\1</a>', $text) != $text){ 
  $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\\1">\\1</a>', $text);
  return $text;
 } 
 $text = eregi_replace('(www\.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="http://\\1">\\1</a>', $text); // ([[:space:]()[{}]) deleted from beginnig of regex 
 $text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})', '<a href="mailto:\\1">\\1</a>', $text); 
 return $text; 
}
1

2 Answers 2

2

Parsing HTML is unestable, expensive and, in a word, a hell. You should use Simple HTML DOM Parser.

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

Comments

1
<?php
    class HtmlLinkUtility
    {
        public static $BaseDomain = null;
        public static function ReplaceEmailToHtmlLink($source)
        {
            return preg_replace('/([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})/i',
                '<a href="mailto:\1">\1</a>', $source);
        }

        public static function ReplaceUrlToHtmlLink($source)
        {
            function replaceUrl($groups) {
                $url = $groups[1];
                return '<a href="' . $url . '"' . (strpos($url, HtmlLinkUtility::$BaseDomain) !== false ?
                    ' target="_blank"' : '') . '>' . $url . '</a>';
            }

            return preg_replace_callback('!(((f|ht){1}tp://)[-a-zA-Z0-9@:%_+.~#?&//=]+)!i',
                replaceUrl, $source);
        }

        public static function ReplaceTextDataToLinks($source, $baseDomain)
        {
            self::$BaseDomain = $baseDomain;
            return self::ReplaceUrlToHtmlLink(self::ReplaceEmailToHtmlLink($source));
        }
    }

    echo HtmlLinkUtility::ReplaceTextDataToLinks(
        "[email protected]<br />http://www.google.com/<br />http://www.test.com/",
        "google.com"
    );
?>

Could not see why you would use two expressions that basically matched/replaced the same thing. Simplified your method a little.

Also, for the record. HTML is not regular in that way that it in any form could be parsed by a regular expression. Though, for simple cases like the one above, it works well.

3 Comments

I need to match the difference between example.com (my) and example1.net(external)
But it still does not work as I want it to. Give me 2min and I'll give you a fully working example.
Try the new code. It should be able to handle the URL's in the way that you wanted. In the example, "google.com" is the base domain. You would of course switch this with your own.

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.