Could anyone post a link for the code they refer to? Really looking for it, but couldn't find it when I searched the forumsOk, posting it now, be up in 5 minutes
[PHP] BBCode Parser
#21
Posted 25 December 2005 - 04:32 PM
#22
Posted 25 December 2005 - 11:40 PM
http://php.net/manua...tern.syntax.php
And eh, indigo, have you tried searching the tutorial database...?
http://www.pixel2lif...&d=1&ss=smilies
Edited by rc69, 25 December 2005 - 11:44 PM.
#23
Posted 26 December 2005 - 07:21 PM
But can't you guys just find something that works properly, and then post that code? Or do they all work just fine?
#24
Posted 18 March 2006 - 07:28 PM
function BBCode ($string) { $search = array( '#\[b\](.*?)\[/b\]#', '#\[i\](.*?)\[/i\]#', '#\[u\](.*?)\[/u\]#', '#\[img\](.*?)\[/img\]#', '#\[url=(.*?)\](.*?)\[/url\]#', '#\[code\](.*?)\[/code\]#' ); $replace = array( '<b>\\1</b>', '<i>\\1</i>', '<u>\\1</u>', '<img src="\\1">', '<a href="\\1">\\2</a>', '<code>\\1</code>' ); return preg_replace($search.'si', $replace, $string); }
i copied this EXACTLY as is and it didn't work.
[Sat Mar 18 18:23:47 2006] [error] PHP Warning: preg_replace(): Unknown modifier ']' in /home/haydenk/public_html/test/bbcode.php on line 21
[Sat Mar 18 18:23:47 2006] [error] PHP Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset 27 in /home/*/public_html/test/bbcode.php on line 21
[Sat Mar 18 18:23:47 2006] [error] PHP Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset 20 in /home/*/public_html/test/bbcode.php on line 21
[Sat Mar 18 18:23:47 2006] [error] PHP Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset 16 in /home/*/public_html/test/bbcode.php on line 21
[Sat Mar 18 18:23:47 2006] [error] PHP Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset 16 in /home/*/public_html/test/bbcode.php on line 21
[Sat Mar 18 18:23:47 2006] [error] PHP Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset 16 in /home/*/public_html/test/bbcode.php on line 21
got those in the error_log
Acually an even better way to do it would be to have it like so.
function BBCode ($string) { $search = array( '@\[(?i)b\](.*?)\[/(?i)b\]@si', '@\[(?i)i\](.*?)\[/(?i)i\]@si', '@\[(?i)u\](.*?)\[/(?i)u\]@si', '@\[(?i)img\](.*?)\[/(?i)img\]@si', '@\[(?i)url=(.*?)\](.*?)\[/(?i)url\]@si', '@\[(?i)code\](.*?)\[/(?i)code\]@si' ); $replace = array( '<b>\\1</b>', '<i>\\1</i>', '<u>\\1</u>', '<img src="\\1">', '<a href="\\1">\\2</a>', '<code>\\1</code>' ); return preg_replace($search , $replace, $string); }
replaced it with this and it works fine.
http://spaceghost.cc/test/bbcode.php
#25
Posted 18 March 2006 - 07:38 PM
<?php function bbcode ($string) { // All the default bbcode arrays. $bbcode = array( //Text Apperence '#\[b\](.*?)\[/b\]#si' => '<b>\\1</b>', '#\[i\](.*?)\[/i\]#si' => '<i>\\1</i>', '#\[u\](.*?)\[/u\]#si' => '<u>\\1</u>', '#\[s\](.*?)\[/s\]#si' => '<strike>\\1</strike>', //Font Color '#\[color=(.*?)\](.*?)\[/color\]#si' => '<font color="\\1">\\2</font>', //Text Effects '#\[bl\](.*?)\[/bl\]#si' => '<blink>\\1</blink>', '#\[marquee\](.*?)\[/marquee\]#si' => '<marquee>\\1</marquee>', //Other '#\[code\](.*?)\[/ code]#si' => '<div class="bbcode_code_title">CODE:</div><div class="bbcode_code_code">\\1<div>', '#\[url=http://(.*?)\](.*?)\[/url]#si' => '<a href="\\1" target="_blank">\\2</a>', '#\[quote\](.*?)\[/quote\]#si' => '<div class="bbcode_quote_title">CODE:</div><div class="bbcode_quote_quote">\\1<div>', '#\[img\](.*?)\[/img\]#si' => '<img src="\\1">', '#\[email\](.*?)\[/email\]#si' => '<a href="mailto:\\1">\\1</a>' ); $output = preg_replace(array_keys($bbcode), array_values($bbcode), $string); return $output; } ?>
NOTCE: there is a space for the [/code] part so it looks like this [/ code] so that it code doesn't look funny. if you use it just take the space out.
It should work then to use the bbcode you would do this:
bbcode('stuff here [b]bold text[/b]');
So hope that helps you guys out.
dEcade
Edited by dEcade, 18 March 2006 - 07:40 PM.
#26
Posted 18 March 2006 - 09:35 PM
Can anybody tell me how to add the smilie bbcode?
#27
Posted 19 March 2006 - 09:07 AM
<?php function set_smiley($string) { $new_string = str_replace(':)', 'SMILEY HTML HERE'); return $new_string; } ?>
Then you would run the script when you posted it so something like this:
Note: This is the whole script put together.
<?php function set_smiley($string) { $new_string = str_replace(':)', 'SMILEY HTML HERE'); return $new_string; } if(isset($_POST['submit']) { $post = set_smiley($_POST['post']); // Notice your text area must be named post. echo $post; } else { echo 'Your Forum Goes Here'; } ?>
If you are trying this script out make sure that the submit button is named submit other wise it won't submit. If it doesn't replace the : ) with the SMILEY HTML HERE or your html then try a preg_replace or something. This script is not tested so it may not work.
dEcade
#28
Posted 21 March 2006 - 05:24 PM
i was thinking there'd be a way to integrated into the bbcode function.
#29
Posted 22 March 2006 - 05:56 AM
#30
Posted 26 March 2006 - 05:22 AM
bbcode(set_smiley('text'));
#31
Posted 26 March 2006 - 07:04 AM
#32
Posted 02 March 2007 - 12:51 AM
Acually an even better way to do it would be to have it like so.
function BBCode ($string) { $search = array( '@\[(?i)b\](.*?)\[/(?i)b\]@si', '@\[(?i)i\](.*?)\[/(?i)i\]@si', '@\[(?i)u\](.*?)\[/(?i)u\]@si', '@\[(?i)img\](.*?)\[/(?i)img\]@si', '@\[(?i)url=(.*?)\](.*?)\[/(?i)url\]@si', '@\[(?i)code\](.*?)\[/(?i)code\]@si' ); $replace = array( '<b>\\1</b>', '<i>\\1</i>', '<u>\\1</u>', '<img src="\\1">', '<a href="\\1">\\2</a>', '<code>\\1</code>' ); return preg_replace($search , $replace, $string); }
it will make the tags not so "case sensitive".
So that would still output the image.
The URL BBCode doesnt work for me
I tried this, but it stays the same
[url=http://www.google.com/]GOOGLE[/url]
Edit: I replaced the url BBCODE with this 1 and it works
'@\[url\s*=\s*(.*?)\s*\](.*?)\[\/url\]@si',
Edited by Jonpopnycorn, 07 March 2007 - 11:24 AM.
#33
Posted 23 March 2007 - 11:10 PM
<?php function parse_bbcode($text, $xhtml = true) { $tags = array( '#\[b\](.*?)\[/b\]#si' => ($xhtml ? '<strong>\\1</strong>' : '<b>\\1</b>'), '#\[i\](.*?)\[/i\]#si' => ($xhtml ? '<em>\\1</em>' : '<i>\\1</i>'), '#\[u\](.*?)\[/u\]#si' => ($xhtml ? '<span style="text-decoration: underline;">\\1</span>' : '<u>\\1</u>'), '#\[s\](.*?)\[/s\]#si' => ($xhtml ? '<strike>\\1</strike>' : '<s>\\1</s>'), '#\[color=(.*?)\](.*?)\[/color\]#si' => ($xhtml ? '<span style="color: \\1;">\\2</span>' : '<font color="\\1">\\2</font>'), '#\[img\](.*?)\[/img\]#si' => ($xhtml ? '<img src="\\1" border="0" alt="" />' : '<img src="\\1" border="0" alt="">'), '#\[url=(.*?)\](.*?)\[/url\]#si' => '<a href="\\1" title="\\2">\\2</a>', '#\[email\](.*?)\[/email\]#si' => '<a href="mailto:\\1" title="Email \\1">\\1</a>', '#\[code\](.*?)\[/code\]#si' => '<code>\\1</code>', '#\[align=(.*?)\](.*?)\[/align\]#si' => ($xhtml ? '<div style="text-align: \\1;">\\2</div>' : '<div align="\\1">\\2</div>'), '#\[br\]#si' => ($xhtml ? '<br style="clear: both;" />' : '<br>'), ); foreach ($tags AS $search => $replace) { $text = preg_replace($search, $replace, $text); } return $text; } function parse_smilies($text) { $smilies = array( ':)' => '<img src="images/smilies/smile.gif" border="0" alt="Smile" title="Smile" />', ':(' => '<img src="images/smilies/sad.gif" border="0" alt="Sad" title="Sad" />', ':D' => '<img src="images/smilies/biggrin.gif" border="0" alt="Big Grin" title="Big Grin" />', ':S' => '<img src="images/smilies/confused.gif" border="0" alt="Confused" title="Confused" />' // etc ); foreach ($smilies AS $search => $replace) { $text = str_replace($search, $replace, $text); } return $text; } ?>
<?php echo parse_bbcode('[align=center][b][color=red]Testing[/color][/b][br]'); ?>
<div style="text-align: center;"><strong><span style="color: red;">Testing</span></strong><br style="clear: both;" /></div>
Edited by SecondV, 23 March 2007 - 11:13 PM.
#34
Posted 02 May 2007 - 04:56 PM
but a question why do you have to do
@\[(?i)b\](.*?)\[/(?i)b\]@si
why use those ugly marks?
and why \\1? where does the \\ stand for and the 1?
<b>\\1</b>
Edited by Nightscream, 02 May 2007 - 04:58 PM.
#35
Posted 06 May 2007 - 01:53 AM
Read the above post, and if you want, the next few posts after that. Then to understand the backslashes, first you have to understand Regex. You can read php.net's manual on Patter Syntax for info about that. But i find the whole thing to be confusing when taken in a large dose. So, to answer your question, scroll down to the section on "Back References" and understand that inside of a string, in order to get a single backslash, you need two.
#36
Posted 06 May 2007 - 04:06 PM
#37
Posted 27 August 2007 - 03:07 AM
I did also a bbcode script some time ago, here is what I did, it's almost the same imho:
function bbcode($text) { $text = strip_tags($text, '<br>'); $suche = array('/\[b\](.+?)\[\/b\]/i', '/\[i\](.+?)\[\/i\]/i', '/\[u\](.+?)\[\/u\]/i', '/\[url=(.+?)\](.+?)\[\/url\]/i', '/\[file(.+?)\|(.+?)\]/i', '/\:\)/i', '/\;\)/i', '/\:D/i', '/\:p/i', '/\:\'\(/i', '/\[key\](.+?)\[\/key\]/ie', '/\[quote\](.+?)\[\/quote\]/is', '/\[color=(.+?)\](.+?)\[\/color\]/i'); $code = array('<strong>$1</strong>', '<i>$1</i>', '<u>$1</u>', '<img src="images/bigger.gif" alt="new_window" width="12px" height="12px" /><a href="$1" class="link" target="_blank">$2</a>', '<a href="view.php?id=$1" class="link">$2</a>', '<img src="images/smilies/smile.gif" alt=":)" />', '<img src="images/smilies/wink.gif" alt=";)" />', '<img src="images/smilies/laugh.gif" alt=":D" />', '<img src="images/smilies/tounge.gif" alt=":p" />', '<img src="images/smilies/worried.gif" alt=":\'(" />', '$test->create_serial("$1")', '<br /><div class="quote">$1</div><br />', '<span style="color: $1;">$2</span>'); $text = stripslashes($text); $text = preg_replace($suche, $code, $text); $text = nl2br($text); return $text; }
Edited by Balor, 27 August 2007 - 03:07 AM.
#38
Posted 31 August 2007 - 09:38 PM
Now how would you turn plain http://mysite.com text into a link, just like on this forum, without messing up the url bbcode
i have created an OOP BBCode Parser that also auto-parses links with and without the [url] tag.
inside the main class i have
// Automatically parse urls $code = preg_replace_callback("#(^|\s|>)((http|https|news|ftp)://\w+[^\s\[\]\<]+)#i", array( &$this, 'autourl' ), $code);
and to complete this, i have a function called autourl, that formats the url into forum-friendly and user-friendly urls.
#39 _*Ultimate`_*
Posted 09 January 2008 - 02:11 AM
#40
Posted 23 June 2008 - 02:04 AM
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users