jaymc Posted November 27, 2007 Share Posted November 27, 2007 I need to turn this $mystring "This can be any txt at all but may contain a url like www.site.com of which needs parsing" into This can be any txt at all but may contain a url like <a href="http://www.site.com target=BlanK>www.site.com</a> of which needs parsing I found the below php code in this section, but it only detects a urls presence within the string, doesnt actually do anything with it if (preg_match('~ (?:[a-z0-9+.-]+://)? # A optional resource (http, ftp, etc) (?:\w+\.)+ # the host.domain \w{2,6} # the TLD \S* # an optional path/to/file ~ix', $mystring)) { echo "no links, you turkey"; } else { echo "nice comment"; } If someone can help me with this I will be really happy! Driving me nuts! If you want to go the extra mile, I need it in javascript aswell, but its more important php method works before anything else Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted November 27, 2007 Share Posted November 27, 2007 $str = preg_replace('/((ftp|(http(s)?)://)?www\.(.*)+\.(.*)+ /','<a href="$1">$1</a>',$str); try that... Quote Link to comment Share on other sites More sharing options...
jaymc Posted November 27, 2007 Author Share Posted November 27, 2007 Almost there.. Your code gives this Warning: preg_replace() [function.preg-replace]: Unknown modifier Hope you can rectify, I am useless at regex Quote Link to comment Share on other sites More sharing options...
jaymc Posted November 28, 2007 Author Share Posted November 28, 2007 Any ideas guys? Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted November 28, 2007 Share Posted November 28, 2007 $str = preg_replace('/(((ftp|http|https):\/\/)?www\.(.*)+\.(.*)+) /','<a href="$1">$1</a>',$str); try that Quote Link to comment Share on other sites More sharing options...
jaymc Posted November 28, 2007 Author Share Posted November 28, 2007 Doesnt output anything, if I take the link out, it prints the text fine Obviously a glitch in code Here, best to use live example and modify until working $str = "dss s sd s www.site.com but may not work"; $str = preg_replace('/(((ftp|http|https):\/\/)?www\.(.*)+\.(.*)+) /','<a href="$1">$1</a>',$str); echo $str; Hope someone can help Quote Link to comment Share on other sites More sharing options...
jaymc Posted November 30, 2007 Author Share Posted November 30, 2007 Any ideas? Quote Link to comment Share on other sites More sharing options...
jcd Posted November 30, 2007 Share Posted November 30, 2007 This does the trick I believe: <?php $str = "dss s sd s www.site.com but may not work"; $str = preg_replace('/\b(?:[a-z0-9+.-]+:\/\/)?((?:www\.)(?:\w+\.)+\w{2,6}\S*)\b/','<a href="$1">$1</a>',$str); echo $str; ?> Quote Link to comment Share on other sites More sharing options...
jcd Posted November 30, 2007 Share Posted November 30, 2007 Er, looks like I put a stray www in there. Change to: $str = preg_replace('/\b((?:[a-z0-9+.-]+:\/\/)?(?:\w+\.)+\w{2,6}\S*)/','<a href="$1">$1</a>',$str); Tried to edit above post but the edit button has disappeared... Quote Link to comment Share on other sites More sharing options...
jaymc Posted November 30, 2007 Author Share Posted November 30, 2007 Great! That works perfect Can anyone do that in javascript now? Be amazing if could Quote Link to comment Share on other sites More sharing options...
jcd Posted November 30, 2007 Share Posted November 30, 2007 http://www.regular-expressions.info/javascriptexample.html Works fine with the above expression. Insert without the / at the start and end. Quote Link to comment Share on other sites More sharing options...
jaymc Posted December 3, 2007 Author Share Posted December 3, 2007 Cheers 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.