MDanz Posted August 4, 2010 Share Posted August 4, 2010 when posting a new message on my forum and it contains a url. How do i automatically add the bbcode url tags to a url in the message? Quote Link to comment https://forums.phpfreaks.com/topic/209751-bbcode-help/ Share on other sites More sharing options...
Adam Posted August 4, 2010 Share Posted August 4, 2010 That depends. Are you using the traditional title syntax? If so, you could use preg_replace with a regex like: $str = preg_replace('/\[url=http://(.+?)\](.+?)\[/url\]/', '<a href="$1">$2</a>', $str); Edit: remove "http://" from the regex, for some reason the forum's adding it in. Quote Link to comment https://forums.phpfreaks.com/topic/209751-bbcode-help/#findComment-1094971 Share on other sites More sharing options...
MDanz Posted August 4, 2010 Author Share Posted August 4, 2010 i'm using these url tags, with the url inbetween. [url][/url] Quote Link to comment https://forums.phpfreaks.com/topic/209751-bbcode-help/#findComment-1094978 Share on other sites More sharing options...
Adam Posted August 4, 2010 Share Posted August 4, 2010 Just need to modify the regex a little: $str = preg_replace('/\[url\](.+?)\[\/url\]/', '<a href="$1">$1</a>', $str); Quote Link to comment https://forums.phpfreaks.com/topic/209751-bbcode-help/#findComment-1094983 Share on other sites More sharing options...
MDanz Posted August 4, 2010 Author Share Posted August 4, 2010 thanks for the help... ok i'm trying this but it is not working. It doesn't turn the text into a hyperlink. $info = mysql_real_escape_string($_POST['content']); $data = preg_replace('/\[url\](.+?)\[\/url\]/', '<a href="$1">$1</a>', $info); echo $data; $info is e.g. www.google.com ... when i put mouseover it , it's not a hyperlink Quote Link to comment https://forums.phpfreaks.com/topic/209751-bbcode-help/#findComment-1094985 Share on other sites More sharing options...
Adam Posted August 4, 2010 Share Posted August 4, 2010 After you define $info, can you just add in var_dump($info); and post the output? I did a quick test with the code below and it worked. $info = '[url=http://google.co.uk]http://google.co.uk[/url]'; $data = preg_replace('/\[url\](.+?)\[\/url\]/', '<a href="$1">$1</a>', $info); echo $data; Quote Link to comment https://forums.phpfreaks.com/topic/209751-bbcode-help/#findComment-1094999 Share on other sites More sharing options...
MDanz Posted August 4, 2010 Author Share Posted August 4, 2010 here's the var dump. string(19) "www.google.com test" it should add the url tags around the google url. Quote Link to comment https://forums.phpfreaks.com/topic/209751-bbcode-help/#findComment-1095000 Share on other sites More sharing options...
Adam Posted August 4, 2010 Share Posted August 4, 2010 I see, sorry I misread your OP. I thought you were wanting to convert BBCode URL tags to a link. So you want to look for valid URLs and convert to url here? thanks for the help... ok i'm trying this but it is not working. It doesn't turn the text into a hyperlink. (...) $info is e.g. www.google.com ... when i put mouseover it , it's not a hyperlink Of course converting the URLs to BBCode tags won't make the link clickable, you'd need to convert the URL to a hyper-link for that. Can you just confirm which output you're after? Quote Link to comment https://forums.phpfreaks.com/topic/209751-bbcode-help/#findComment-1095001 Share on other sites More sharing options...
MDanz Posted August 4, 2010 Author Share Posted August 4, 2010 yes to your 1st question. i have this bbcode which works if i put the url tags around a url. function BBCode($string){ $string = nl2br(htmlspecialchars($string)); $patterns = array( '`\[b\](.+?)\[/b\]`is', '`\[i\](.+?)\[/i\]`is', '`\[u\](.+?)\[/u\]`is', '`\[strike\](.+?)\[/strike\]`is', '`\[color=#([0-9]{6})\](.+?)\[/color\]`is', '`\[email\](.+?)\[/email\]`is', '`\[img\](.+?)\[/img\]`is', '`\[url=([a-z0-9]+://)([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*?)?)\](.*?)\[/url\]`si', '`\[url\]([a-z0-9]+?://){1}([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*)?)\[/url\]`si', '`\[url\]((www|ftp)\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*?)?)\[/url\]`si', '`\[flash=([0-9]+),([0-9]+)\](.+?)\[/flash\]`is', '`\[quote\](.+?)\[/quote\]`is', '`\[indent](.+?)\[/indent\]`is', '`\[size=([1-6]+)\](.+?)\[/size\]`is' ); $replaces = array( '<strong>\\1</strong>', '<em>\\1</em>', '<span style="border-bottom: 1px dotted">\\1</span>', '<strike>\\1</strike>', '<span style="color:#\1;">\2</span>', '<a href="mailto:\1">\1</a>', '<img src="\1" alt="" style="border:0px;" />', '<a href="\1\2">\6</a>', '<a href="\1\2">\1\2</a>', '<a href="http://\1">\1</a>', '<object width="\1" height="\2"><param name="movie" value="\3" /><embed src="\3" width="\1" height="\2"></embed></object>', '<strong>Quote:</strong><div style="margin:0px 10px;padding:5px;background-color:#000000;border:1px dotted #CCCCCC;width:80%;"><em>\1</em></div>', '<pre>\\1</pre>', '<h\1>\2</h\1>' ); $string = preg_replace($patterns, $replaces , $string); return $string; } but that only works if i put the url tags around the url. how do i put the tags around a url automatically... like this forum. Quote Link to comment https://forums.phpfreaks.com/topic/209751-bbcode-help/#findComment-1095002 Share on other sites More sharing options...
Adam Posted August 4, 2010 Share Posted August 4, 2010 Gotchya! Okay, you need a regex then that is able to match valid URLs. I'm not going to write one because there's so many examples available on the net, and they can get complicated. Using a random one I came across: $info = 'http://google.co.uk'; $data = preg_replace('/((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?)/', '[url=http://$1]$1[/url]', $info); echo $data; Again need to remove the "http://" in the second parameter, the forum's adding it in for some reason. Quote Link to comment https://forums.phpfreaks.com/topic/209751-bbcode-help/#findComment-1095005 Share on other sites More sharing options...
MDanz Posted August 4, 2010 Author Share Posted August 4, 2010 THANKS!! it works. Quote Link to comment https://forums.phpfreaks.com/topic/209751-bbcode-help/#findComment-1095012 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.