justcrapx Posted November 24, 2006 Share Posted November 24, 2006 Hi, im new in PHP. I simply made a php page to fetch some data from the database and show it, but i could not find an efficient way to turn urls into links. I tried using str_replace() function but could not make it. All i need is to show all urls as links! Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted November 24, 2006 Share Posted November 24, 2006 simply echo the url into the href attribute of the a tag! Quote Link to comment Share on other sites More sharing options...
justcrapx Posted November 25, 2006 Author Share Posted November 25, 2006 Ah sorry but guess i could not mention the point. The entry is not mere url. Its just text and it has some urls in it. just like...."Here is some text, and i want to show the followed url as a link : www.phpfreaks.com " Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted November 25, 2006 Share Posted November 25, 2006 again, like he said echo out ech o "<a href="url" title="title">Name</a>You could do this dynamically, just send the titel the array you saved the mysql fetch array in. Quote Link to comment Share on other sites More sharing options...
justcrapx Posted November 25, 2006 Author Share Posted November 25, 2006 Ah, is my english so bad? I know how to echo query results and how to use anchor tags. What i really cant figure out is how to extract urls from a complete text.Just pretend theres a database entry like: "hello dude, check this website. www.phpfreaks. com"when i fetch this entry inside anchor tags it becomes something like,<a href="hello dude, check this website. www.phpfreaks.com">but i want the following output.<p>hello dude, check this website.</p><a href="www.phpfreaks.com">www.phpfreaks.com</a>Well i hope this time i could explain the matter. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted November 25, 2006 Share Posted November 25, 2006 Ah ok, I see what you meanregular expressions, you would have to use regular expressions to hunt down the link, and change it to an anchor, have it place a <a href=" before it, and then have hte link, then have it put a " title="if you want one">name here</a>you have to have it insert all of that where you want using string replace, and search and find fucntions, comparing strings, you are tlaking about working with regular expressions, above what I know about regular expressions, but that is going tobe where you want to look. Quote Link to comment Share on other sites More sharing options...
freeloader Posted November 25, 2006 Share Posted November 25, 2006 fetch from mysql$a=preg_match("/\bwww.([A-Za-z0-9._-]+).com\b.*/i",$queryresultstring,$b);$url = "www.$b[1].com"; Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted November 25, 2006 Share Posted November 25, 2006 See, there you go, the script that guy gave you shoudl work totally.Regular expressions are powerful Quote Link to comment Share on other sites More sharing options...
justcrapx Posted November 25, 2006 Author Share Posted November 25, 2006 Ok thanks. Hop i can figure it out. Quote Link to comment Share on other sites More sharing options...
justcrapx Posted November 25, 2006 Author Share Posted November 25, 2006 Guess it will be neccessary and a hard thing to expand that code, cuz links are not always that simple. Just like the following.http://www.phpfreaks.com/forums/index.php?action=post;topic=116205.0;num_replies=8 Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted November 25, 2006 Share Posted November 25, 2006 His should work with everything, if not, then just expand it a little.If you are having problems.Integrate what he showed you into your code, test it out, see where it work's, adn where it doesn't see what you can do, then bring it back here, post the entire code again, and tell us what it does, what it doesn't do, and what you need help with.Then you can go from there. Quote Link to comment Share on other sites More sharing options...
freeloader Posted November 25, 2006 Share Posted November 25, 2006 [code]$a=preg_match("/\bhttp:\/\/([A-Za-z0-9._-]+) \b.*/i",$string,$b);$url = "http://$b[1]";[/code]then use this and tell us next time how your URL looks like ;) Quote Link to comment Share on other sites More sharing options...
justcrapx Posted November 25, 2006 Author Share Posted November 25, 2006 [code]<?php$mssage = "this is an url : http://www.blank.com";$a=preg_match("/\bhttp:\/\/([A-Za-z0-9._-]+) \b.*/i",$mssage,$b);$url = "http://$b[1]";echo $url;?>[/code]OutputNotice: Undefined index: 1 in C:\Webserver\Apache2\htdocs\parse_url.php on line 9http:// Quote Link to comment Share on other sites More sharing options...
freeloader Posted November 25, 2006 Share Posted November 25, 2006 Tell us more about the string. How does it look like? Is the url always at the end of a sentence? Is it always a .com? Does it always start with http:// or www? Will one string contain more then one URL? Quote Link to comment Share on other sites More sharing options...
justcrapx Posted November 25, 2006 Author Share Posted November 25, 2006 The string is always a message just like each one in this forum topic. So i dont have the chance to know the scheme, domain name or path everytime. What i needed was some code to fetch any link inside the string between anchor tags and to target the link itself. Finally i managed to find a way. That works for any probable url for the http:// or www scheme.[code]<?php$mssage = preg_replace('=([^\s]*)(www\.)=', ' http://www.', $mssage);$mssage = preg_replace('=([^\s]*)(\w://[www\.]*)([^\s]*)=', '<a href="\\1\\2\\3\\4" target=_blank>\\1\\2\\3\\4</a>', $mssage);return $mssage;?>[/code] Quote Link to comment 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.