mmsolutions Posted January 19, 2010 Share Posted January 19, 2010 Dear all I have the following code: <?php $query="select * from website_pages_sub WHERE subpage_id = ".$_SESSION['mainpage'] ." AND subpage_status LIKE '%live%'"; $result = mysql_query($query); while ($row = mysql_fetch_array($result)) { ?> <?php echo $row['content1';?> Content1 is a field within a row in a table as you know but it contains lots of text like a new entry. Within this field of text could be something like: "Contact the club website at http://www.google.co.uk" I want code to assess the contents of this field and if it finds a website entry to turn it into a url. Can this be done ? Kind reagards Mark Quote Link to comment https://forums.phpfreaks.com/topic/189023-make-website-addresses-into-hyperlinks-within-a-variable/ Share on other sites More sharing options...
ShadowIce Posted January 19, 2010 Share Posted January 19, 2010 Link should be like this: http://google.co.uk?myvar=1 Quote Link to comment https://forums.phpfreaks.com/topic/189023-make-website-addresses-into-hyperlinks-within-a-variable/#findComment-997990 Share on other sites More sharing options...
Adam Posted January 19, 2010 Share Posted January 19, 2010 You'd want to use regexp for something like this. This may be helpful: preg_replace('\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]', '<a href="\0">\0</a>', $text); Taken from: http://www.roscripts.com/PHP_regular_expressions_examples-136.html -- not tested. Read up on preg_replace for how to use it properly. Quote Link to comment https://forums.phpfreaks.com/topic/189023-make-website-addresses-into-hyperlinks-within-a-variable/#findComment-998005 Share on other sites More sharing options...
mmsolutions Posted January 19, 2010 Author Share Posted January 19, 2010 So in the following instance, what do we replace $foo with ? is it $content ? <?php function autolink_test($foo) { // Modified from: http://www.totallyphp.co.uk/code/convert_links_into_clickable_hyperlinks.htm $foo = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\\1">\\1</a>', $page_content1); if( strpos($foo, "http") === FALSE ) { $foo = eregi_replace('(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="http://\\1" target=_blank rel=nofollow >\\1</a>', $page_content1); } else { $foo = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\\1<a href="http://\\2" target=_blank rel=nofollow >\\2</a>', $page_content1); } $foo = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})', '<a href="mailto:\\1">\\1</a>', $page_content1); return $foo; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/189023-make-website-addresses-into-hyperlinks-within-a-variable/#findComment-998111 Share on other sites More sharing options...
Adam Posted January 19, 2010 Share Posted January 19, 2010 The ereg* functions are deprecated as of PHP5-3. I'd go with a preg_* solution if I were you. Quote Link to comment https://forums.phpfreaks.com/topic/189023-make-website-addresses-into-hyperlinks-within-a-variable/#findComment-998129 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.