stelthius Posted May 30, 2009 Share Posted May 30, 2009 Ok so to be short and sweet im using this for my bbcode and it all works fine other than link i cant seem to get link to work for some odd reason it keeps printing out plain html <?php function parseBB($text) { $pattern[0] = "/\[b\](.*?)\[\/b\]/is"; // Bold $pattern[1] = '/\[i\](.*?)\[\/i\]/is'; // Italic $pattern[2] = '/\[u\](.*?)\[\/u\]/is'; // Underline $pattern[3] = '/\[url\](.*?)\](.*?)\[\/url\]/is'; // Link $pattern[4] = '/\[img\](.*?)\[\/img\]/is'; // Image $pattern[5] = '/\[color=(.*?)](.*?)\[\/color\]/is'; // Color $replace[0] = "<span style='font-weight: bold;'>$1</span>"; $replace[1] = "<span style='font-style: italic;'>$1</span>"; $replace[2] = "<span style='text-decoration: underline;'>$1</span>"; $replace[3] = "<a href='$1'>$2</a>"; $replace[4] = "<img src='$1' />"; $replace[5] = "<span style='color: $1;'>$2</span>"; $text = preg_replace($pattern, $replace, $text); return $text; } ?> Any ideas are greatly appretiated. *EDIT* sorry forgot to mention i also have tried oing it this way <?php function parseBB($text) { $pattern[0] = "/\[b\](.*?)\[\/b\]/is"; // Bold $pattern[1] = '/\[i\](.*?)\[\/i\]/is'; // Italic $pattern[2] = '/\[u\](.*?)\[\/u\]/is'; // Underline $pattern[3] = '/\[url=http://(.*?)\](.*?)\[\/url\]/is'; // Link $pattern[4] = '/\[img\](.*?)\[\/img\]/is'; // Image $pattern[5] = '/\[color=(.*?)](.*?)\[\/color\]/is'; // Color $replace[0] = "<span style='font-weight: bold;'>$1</span>"; $replace[1] = "<span style='font-style: italic;'>$1</span>"; $replace[2] = "<span style='text-decoration: underline;'>$1</span>"; $replace[3] = "<a href='$1'>$2</a>"; $replace[4] = "<img src='$1' />"; $replace[5] = "<span style='color: $1;'>$2</span>"; $text = preg_replace($pattern, $replace, $text); return $text; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/160257-solved-bbcode-question/ Share on other sites More sharing options...
Garethp Posted May 30, 2009 Share Posted May 30, 2009 $pattern[3] = '/\[url\](.*?)\](.*?)\[\/url\]/is'; Your pattern is set up for TEXT]INSERTHERE]TEXT by the looks of it. Try $pattern[3] = '/\[url\=(.*?)\](.*?)\[\/url\]/is'; Or see if you can add the BBCode Extention to your PHP to do it for you Quote Link to comment https://forums.phpfreaks.com/topic/160257-solved-bbcode-question/#findComment-845680 Share on other sites More sharing options...
stelthius Posted May 30, 2009 Author Share Posted May 30, 2009 Hello Garethp, im using <a title="Bold : [url\=[/url]" onmousedown="addTags(\'[url\=\',\'[/url]\')" style="background-color:#F7F7F7; font-size:10pt; border: outset white 1px;"> <b>URL</b> </a> To insert my code i've just tried what you said but it still prints out [url=link test[/url] in the PM am i doing something wrong maybe im putting the bbcode tag into the JS button wrong ? Quote Link to comment https://forums.phpfreaks.com/topic/160257-solved-bbcode-question/#findComment-845683 Share on other sites More sharing options...
Garethp Posted May 30, 2009 Share Posted May 30, 2009 I tested your code. A) You forgot to escape the // in your http:// in $Pattern[3] and your test is "Link Test" not "http://Linktest.com" Quote Link to comment https://forums.phpfreaks.com/topic/160257-solved-bbcode-question/#findComment-845684 Share on other sites More sharing options...
Garethp Posted May 30, 2009 Share Posted May 30, 2009 If you try [url=http://Google.com]Google[/url] With $pattern[3] = '/\[url=http://http:\/\/(.*?)\](.*?)\[\/url\]/is'; Then it works However, you may want to change $replace[3] = "<a href='$1'>$2</a>"; to $replace[3] = "<a href='http://$1'>$2</a>"; Otherwise the link won't lead off your site Quote Link to comment https://forums.phpfreaks.com/topic/160257-solved-bbcode-question/#findComment-845686 Share on other sites More sharing options...
stelthius Posted May 30, 2009 Author Share Posted May 30, 2009 Yes i understand all that clearly i made a mistake with my replacement vars, but with it still printing out as html its got to be my button that im doing wrong.. <a title="Bold : [url\=[/url]" onmousedown="addTags(\'[url\=\',\'[/url]\')" style="background-color:#F7F7F7; font-size:10pt; border: outset white 1px;"> <b>URL</b> </a> Sorry if i seem dull first time using BBcode and JS to insert into textarea, Also thanks for your help & time its appretiated. Quote Link to comment https://forums.phpfreaks.com/topic/160257-solved-bbcode-question/#findComment-845695 Share on other sites More sharing options...
Garethp Posted May 30, 2009 Share Posted May 30, 2009 Well, when you use that javascript, what does the textarea show before you run it through the PHP? And don't worry about seeming dull, we all start off like you, that's why we have this forum Quote Link to comment https://forums.phpfreaks.com/topic/160257-solved-bbcode-question/#findComment-845699 Share on other sites More sharing options...
stelthius Posted May 30, 2009 Author Share Posted May 30, 2009 Ok when i type in my URL and then wrap it with the URL tags i get [url=www.google.co.uk[/url] < Is what it looks like in the text area before i continue to send the message to user X Now this is whats confusing me as ive seen lots of places using the URL tag that make it look like that... Quote Link to comment https://forums.phpfreaks.com/topic/160257-solved-bbcode-question/#findComment-845708 Share on other sites More sharing options...
Garethp Posted May 30, 2009 Share Posted May 30, 2009 Hm... well, your JS is the wrong format then, look over it, it needs to output like this Text Quote Link to comment https://forums.phpfreaks.com/topic/160257-solved-bbcode-question/#findComment-845711 Share on other sites More sharing options...
stelthius Posted May 30, 2009 Author Share Posted May 30, 2009 well i figured using <a title="Bold : [url\=[/url]" onmousedown="addTags(\'[url\=\',\'[/url]\')" style="background-color:#F7F7F7; font-size:10pt; border: outset white 1px;"> <b>URL</b> </a> would be the correct way t do it but im wrong so it seems.. ill continue to tr and work it out thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/160257-solved-bbcode-question/#findComment-845712 Share on other sites More sharing options...
Garethp Posted May 30, 2009 Share Posted May 30, 2009 Well show me the addTags() function, let's see how it works Quote Link to comment https://forums.phpfreaks.com/topic/160257-solved-bbcode-question/#findComment-845714 Share on other sites More sharing options...
kickstart Posted May 30, 2009 Share Posted May 30, 2009 Hi Not sure if I am missing something, but the javascript seems to be inserting [url=[/url] (for example) where I would expect it to insert [url=][/url] All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/160257-solved-bbcode-question/#findComment-845717 Share on other sites More sharing options...
stelthius Posted May 30, 2009 Author Share Posted May 30, 2009 This works fine as im using it to insert bold, italic, underline and image tags and they work perfectly, maybe im wrong and missed something out.. <script language="JavaScript" type="text/JavaScript"> function addText(Text) { var obj = document.send.message; obj.focus(); if (document.selection && document.selection.createRange) // Internet Explorer { sel = document.selection.createRange(); if (sel.parentElement() == obj) sel.text = Text; } else if (obj != "undefined") // Firefox { var longueur = parseInt(obj.textLength); var selStart = obj.selectionStart; var selEnd = obj.selectionEnd; obj.value = obj.value.substring(0,selStart) + Text + obj.value.substring(selEnd,longueur); } else obj.value += Text; obj.focus(); } function addTags(Tag, fTag) { var obj = document.send.message; obj.focus(); if (document.selection && document.selection.createRange) // Internet Explorer { sel = document.selection.createRange(); if (sel.parentElement() == obj) sel.text = Tag + sel.text + fTag; } else if (obj != "undefined") // Firefox { var longueur = parseInt(obj.textLength); var selStart = obj.selectionStart; var selEnd = obj.selectionEnd; obj.value = obj.value.substring(0,selStart) + Tag + obj.value.substring(selStart,selEnd) + fTag + obj.value.substring(selEnd,longueur); } else obj.value += Tag + fTag; obj.focus(); } </script> Quote Link to comment https://forums.phpfreaks.com/topic/160257-solved-bbcode-question/#findComment-845719 Share on other sites More sharing options...
Garethp Posted May 30, 2009 Share Posted May 30, 2009 Here's your problem if (sel.parentElement() == obj) sel.text = Tag + sel.text + fTag; A) You have two variables, while sel.text is only one B) fTag is [/url] Not ]Link[/url] Change [code] onmousedown="addTags(\'[url\=\',\'[/url]\')" To onmousedown="addTags(\'[url\=\',\']Link[/url]\')" Quote Link to comment https://forums.phpfreaks.com/topic/160257-solved-bbcode-question/#findComment-845723 Share on other sites More sharing options...
stelthius Posted May 30, 2009 Author Share Posted May 30, 2009 I tried something similair to that earlier and it gave me Warning: preg_replace() [function.preg-replace]: Unknown modifier '/' in /home/phpcorec/public_html/classes/bbcode.class.php on line 18 the same as its just given me now when i replaced onmousedown="addTags(\'[url\=\',\'[/url]\')" with onmousedown="addTags(\'[url\=\',\']Link[/url]\')" Quote Link to comment https://forums.phpfreaks.com/topic/160257-solved-bbcode-question/#findComment-845726 Share on other sites More sharing options...
Garethp Posted May 30, 2009 Share Posted May 30, 2009 Well, can you show us the output to the Textarea, before it goes through the PHP? Quote Link to comment https://forums.phpfreaks.com/topic/160257-solved-bbcode-question/#findComment-845729 Share on other sites More sharing options...
kickstart Posted May 30, 2009 Share Posted May 30, 2009 Hi Not sure the "/" needs to be escaped with a "\" in your bbcode.php. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/160257-solved-bbcode-question/#findComment-845734 Share on other sites More sharing options...
stelthius Posted May 30, 2009 Author Share Posted May 30, 2009 [ url=http://]Link[ /url] is the output to my text area before it is submitted. Quote Link to comment https://forums.phpfreaks.com/topic/160257-solved-bbcode-question/#findComment-845739 Share on other sites More sharing options...
Garethp Posted May 30, 2009 Share Posted May 30, 2009 Ok, can you show us your current PHP file then? If the problem isn't there, I can't help you Quote Link to comment https://forums.phpfreaks.com/topic/160257-solved-bbcode-question/#findComment-845742 Share on other sites More sharing options...
stelthius Posted May 30, 2009 Author Share Posted May 30, 2009 Here is my current BBcode PHP file, changes made as suggested by you, <?php function parseBB($text) { $pattern[0] = "/\[b\](.*?)\[\/b\]/is"; // Bold $pattern[1] = '/\[i\](.*?)\[\/i\]/is'; // Italic $pattern[2] = '/\[u\](.*?)\[\/u\]/is'; // Underline $pattern[3] = '/\[url=http://http:\/\/(.*?)\](.*?)\[\/url\]/is'; // Link $pattern[4] = '/\[img\](.*?)\[\/img\]/is'; // Image $pattern[5] = '/\[color=(.*?)](.*?)\[\/color\]/is'; // Color $replace[0] = "<span style='font-weight: bold;'>$1</span>"; $replace[1] = "<span style='font-style: italic;'>$1</span>"; $replace[2] = "<span style='text-decoration: underline;'>$1</span>"; $replace[3] = "<a href='http://$1'>$2</a>"; $replace[4] = "<img src='$1' />"; $replace[5] = "<span style='color: $1;'>$2</span>"; $text = preg_replace($pattern, $replace, $text); return $text; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/160257-solved-bbcode-question/#findComment-845761 Share on other sites More sharing options...
Garethp Posted May 30, 2009 Share Posted May 30, 2009 Here's your problem $pattern[3] = '/\[url=http://http:\/\/(.*?)\](.*?)\[\/url\]/is'; [code] Has to become [code] $pattern[3] = '/\[url=http:\/\/(.*?)\](.*?)\[\/url\]/is'; You added another http:// which would have caused a problem in the first place, and you forgot to put a \ infront of the /'s. The one I gave you should work Quote Link to comment https://forums.phpfreaks.com/topic/160257-solved-bbcode-question/#findComment-845765 Share on other sites More sharing options...
stelthius Posted May 30, 2009 Author Share Posted May 30, 2009 If you try [url=http://Google.com]Google[/url] With $pattern[3] = '/\[url=http://http:\/\/(.*?)\](.*?)\[\/url\]/is'; Sorry i copied exactly what you said i guess i should have realised you didnt mean to type http:// two times, but, none the less thanks for the help as it is now working fully. thank you, no chocolate for you tho email wont send it hehe Quote Link to comment https://forums.phpfreaks.com/topic/160257-solved-bbcode-question/#findComment-845766 Share on other sites More sharing options...
Garethp Posted May 30, 2009 Share Posted May 30, 2009 Hahah, thanks anyway. Someday people will start emailing me chocolates. Maybe Lindt. Oh well, that's a fair bit away Quote Link to comment https://forums.phpfreaks.com/topic/160257-solved-bbcode-question/#findComment-845767 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.