Matching sub-domains

Hi,

Currently I have this regex that matched www url just fine

preg_replace("#((http|ftp|https))?:(\\\\/\\\\/)(www\\.[^ \\"\	\
\\r<]+)#", "<a href=\\"\\\\1://\\\\4\\" target=\\"_blank\\"></a>", $text );

How do I add to match:

http://s3.amazonaws.com/data.tumblr.com

etc?

I tried replace the www with [a-z0-9]+ but can’t work :frowning:

Any help greatly appreciated.

Just removing www\. should do the trick :slight_smile:

Edit: Ok I’ve kinda make it work the first part such that the links are built, but the issue is the 2nd regex issue during populating.

$text = 'Some of my favorites images from browsing this week. Enjoy! LINKS: Watermelon Shot: http://www.dailymail.co.uk/news/article-1061431/Do-try-home-From-wing-walking-granny-washing-machine-Houdini-worlds-craziest-stunts.html inverted sandwich: http://9gag.com/gag/144972/ Love Poem: http://9gag.com/gag/144734/ Fool People: http://www.someecards.com/usercards/viewcard/679c2237affe479b5ef6616e9414978e sad cat: http://kuvaton.com/browse/18369/poor_cat.jpg unicorn bike! http://s3.amazonaws.com/data.tumblr.com/tumblr_lmrmldJH6a1qzmowao1_1280.jpg?AWSAccessKeyId=AKIAJ6IHWSU3BX3X7X3Q&Expires=1308677960&Signature=sjilb39o4ntEAthHvH6WXtI39yw%3D Jaws swimsuit: http://www.geeksaresexy.net/2011/06/15/shark-vs-mermaids-swimsuit/ Bubble: http://flyingscotsman.tumblr.com/post/6564544284/blowing-bubbles Shocked barn: http://wtface.com/object/popular/1727-the-usual-reaction-after-wa Toilet Laugh: http://wtface.com/object/popular/1780-d Grumpy cookie: http://wtface.com/object/popular/1703-if-you-were-about-to-be-eat acid-face cookie: http://wtface.com/object/popular/1696-quaid-start-the-reactor-f laughing bag: http://wtface.com/object/popular/1729-can-i-have-my-lunch-back face shadow: http://www.moillusions.com/2011/06/garage-door-face-illusion.html beer pong shadow: http://shitbrix.com/mind****/recent/61943-beer-pong NUDE: http://shitbrix.com/mind****/recent/21575-hyndai Final boss of the internet: http://28.media.tumblr.com/tumblr_lmid5yJ6qO1qzpzfmo1_400.jpg Dog Smiles: http://www.buzzfeed.com/mjs538/awkward-dog-smiles Amazing Make-up art: http://katiealves.deviantart.com/ Mirror Lake: http://kuvaton.com/browse/18379/mirror_lake.jpg graffiti: http://kuvaton.com/browse/18380/now_youre_extinct.jpg IRL angry bird attack: http://****yeahalbuquerque.tumblr.com/post/6135754107 gross tree: http://www.dailymail.co.uk/news/article-2001934/Scariest-tree-world-Suffolk-70ft-beech-wonky-eyes.html horse neck shirt: http://theclearlydope.tumblr.com/post/6577239663/its-a-great-first-date-conversation-piece pandas: http://www.telegraph.co.uk/news/worldnews/asia/china/8184168/Researchers-dressed-as-pandas-introduce-cubs-to-wild.html heart hair: http://9gag.com/gag/146213/ glasses: http://kuvaton.com/browse/18377/glasses2.jpg';

$text = preg_replace("#((\\w+://)?(\\w+\\.[^ \\"\	\
\\r<]+))#", '<a href="\\\\1" target="_blank"></a>', $text );

echo $text; exit;

// The problem now is this portion
preg_match("#(<a href=\\"(\\w+://)?([^\\"]+)\\"[^>]+>)(</a>)#", $text, $match );
if( isset( $match[3] ) ) {
    $urlText =  $match[3];
    $text = preg_replace("#(<a href=\\"(\\w+://)?([^\\"]+)\\"[^>]+>)(</a>)#", "\\\\1$urlText\\\\4", $text );
}

echo($text);

I’m getting more and more closer, the only issue now is the last regex not replacing domain name starting with digit? For example: 9gag.com

$text = 'Some of my favorites images from browsing this week. Enjoy! LINKS: Watermelon Shot: http://www.dailymail.co.uk/news/article-1061431/Do-try-home-From-wing-walking-granny-washing-machine-Houdini-worlds-craziest-stunts.html inverted sandwich: http://9gag.com/gag/144972/ Love Poem: http://9gag.com/gag/144734/ Fool People: http://www.someecards.com/usercards/viewcard/679c2237affe479b5ef6616e9414978e sad cat: http://kuvaton.com/browse/18369/poor_cat.jpg unicorn bike! http://s3.amazonaws.com/data.tumblr.com/tumblr_lmrmldJH6a1qzmowao1_1280.jpg?AWSAccessKeyId=AKIAJ6IHWSU3BX3X7X3Q&Expires=1308677960&Signature=sjilb39o4ntEAthHvH6WXtI39yw%3D Jaws swimsuit: http://www.geeksaresexy.net/2011/06/15/shark-vs-mermaids-swimsuit/ Bubble: http://flyingscotsman.tumblr.com/post/6564544284/blowing-bubbles Shocked barn: http://wtface.com/object/popular/1727-the-usual-reaction-after-wa Toilet Laugh: http://wtface.com/object/popular/1780-d Grumpy cookie: http://wtface.com/object/popular/1703-if-you-were-about-to-be-eat acid-face cookie: http://wtface.com/object/popular/1696-quaid-start-the-reactor-f laughing bag: http://wtface.com/object/popular/1729-can-i-have-my-lunch-back face shadow: http://www.moillusions.com/2011/06/garage-door-face-illusion.html beer pong shadow: http://shitbrix.com/mind****/recent/61943-beer-pong NUDE: http://shitbrix.com/mind****/recent/21575-hyndai Final boss of the internet: http://28.media.tumblr.com/tumblr_lmid5yJ6qO1qzpzfmo1_400.jpg Dog Smiles: http://www.buzzfeed.com/mjs538/awkward-dog-smiles Amazing Make-up art: http://katiealves.deviantart.com/ Mirror Lake: http://kuvaton.com/browse/18379/mirror_lake.jpg graffiti: http://kuvaton.com/browse/18380/now_youre_extinct.jpg IRL angry bird attack: http://****yeahalbuquerque.tumblr.com/post/6135754107 gross tree: http://www.dailymail.co.uk/news/article-2001934/Scariest-tree-world-Suffolk-70ft-beech-wonky-eyes.html horse neck shirt: http://theclearlydope.tumblr.com/post/6577239663/its-a-great-first-date-conversation-piece pandas: http://www.telegraph.co.uk/news/worldnews/asia/china/8184168/Researchers-dressed-as-pandas-introduce-cubs-to-wild.html heart hair: http://9gag.com/gag/146213/ glasses: http://kuvaton.com/browse/18377/glasses2.jpg';

$text = preg_replace("#((\\w+://)?(\\w+\\.[^ \\"\	\
\\r<]+))#", '<a href="\\\\1" target="_blank"></a>', $text );

preg_match_all("#(<a href=\\"(\\w+://)?([^\\"]+)\\"[^>]+>)(</a>)#", $text, $match );
if( isset( $match[0] ) ) {
    $sizeof = sizeof( $match[0] );
    if( $sizeof > 0 ) {
        for( $i=0; $i<$sizeof; $i++ ) {
            $urlText = $match[3][$i];
            // Not replacing name starting digit
            $text = preg_replace('#(<a href="(\\w+://)?(' . $urlText . '+)"[^>]+>)(</a>)#', "\\\\1$urlText\\\\4", $text );
        }
    }

}

echo($text);