maddali Posted February 17, 2010 Share Posted February 17, 2010 Hi, Can you guys help me giving a function that takes all the contents in a particular field and returns text and url seperately... For instance, Go: here is the link www.google.com it takes all the contents in the Go,and returns "here is the link" as text and "www.google.com" as link.I've an idea that preg match has something to do with content matching,I've tried that and I gave up. Any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/192433-function-to-return-url/ Share on other sites More sharing options...
bbaker Posted February 17, 2010 Share Posted February 17, 2010 are you just trying to automatically get urls formatted? Quote Link to comment https://forums.phpfreaks.com/topic/192433-function-to-return-url/#findComment-1013950 Share on other sites More sharing options...
maddali Posted February 17, 2010 Author Share Posted February 17, 2010 yes.... Quote Link to comment https://forums.phpfreaks.com/topic/192433-function-to-return-url/#findComment-1013986 Share on other sites More sharing options...
premiso Posted February 17, 2010 Share Posted February 17, 2010 See: http://www.phpfreaks.com/forums/index.php/topic,288273.0.html Quote Link to comment https://forums.phpfreaks.com/topic/192433-function-to-return-url/#findComment-1013998 Share on other sites More sharing options...
maddali Posted February 17, 2010 Author Share Posted February 17, 2010 I've seen that thread and thats not wroking in my case.... here is my funcation takes the text and which should display the text as a text and url as a hyperlink...but this displays both text as well as url as a hyperlink.... function make_url_link($url) { if(empty($url)) { return; } $url= preg_replace("/([^A-z0-9])(http|ftp|https)([\:\/\/])([^\\s]+)/","<a href=\"$2$3$4\">$2$3$4</a>",$url); return '<a href="'.$url.'">'.$url.'</a>'; } any suggestions?? Quote Link to comment https://forums.phpfreaks.com/topic/192433-function-to-return-url/#findComment-1014008 Share on other sites More sharing options...
teamatomic Posted February 17, 2010 Share Posted February 17, 2010 preg_replace('%\b(http|https)://[-A-Z0-9+&@#/?=~_|!:,.;]*[-A-Z0-9+&@#/=~_|]%i', '<a href="\0">\0</a>', $line); HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/192433-function-to-return-url/#findComment-1014057 Share on other sites More sharing options...
maddali Posted February 18, 2010 Author Share Posted February 18, 2010 Thanks it worked... Could you tell me what exactly what exactly the content means inside the replace function...I've some idea that it replaces the url with hyperlink...I'm confused of all the letters and symbols inside the function...Also,could you help me finding some sources on regex and the stuff inside which does the actual trick... Maddali Quote Link to comment https://forums.phpfreaks.com/topic/192433-function-to-return-url/#findComment-1014533 Share on other sites More sharing options...
teamatomic Posted February 19, 2010 Share Posted February 19, 2010 preg_replace('%\b(http|https)://[-A-Z0-9+&@#/?=~_|!:,.;]*[-A-Z0-9+&@#/=~_|]%i', '<a href="\0">\0</a>', $line); I'll make it a bit simple. % is a regex delimiter that must begin and end the regex. It does not need to be % it can be anything that is not alph-numeric and IS NOT used within the regex. So in this case I used % \b is a word boundary, we take it all as one chunk of a word (X|Y) means X or Y so we match http or https followed by :// then followed by [] which denotes a class meaning only what is in the class will be matched. the last part is the i at the end, means case insensitive The significant part is that the classes are missing \S which is a space, so it matches anything in the class unitl a space then stops. because it's a replace it has whats called "look back" whick looks back at what it matched and spits it out. if you look at the first example given there are a lot of round brackets() those cause specific look backs that are $n, corresponding to each successive set of round brackets. in the second example since we have no round brackets(the first set does not count cause its sorta like a conditional(OR)) so with the built in look back we default to 0 for what we matched with the regex to replace. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/192433-function-to-return-url/#findComment-1014594 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.