PHP - Bbcode: Preg_replace Error
I have this function:
function bbcode($input){ $input = strip_tags($input); $input = htmlentities($input); $search = array( '/\[b\](.*?)\[\/b\]/is', '/\[i\](.*?)\[\/i\]/is', '/\[u\](.*?)\[\/u\]/is', '/\[img\](.*?)\[\/img\]/is', '/\[url=http://(.*?)\](.*?)\[\/url\]/is', '/\[font color=(.*?) size=(.*?) face=(.*?)\](.*?)\[\/font\]/is', ); $replace = array( '<b>$1</b>', '<i>$1</i>', '<u>$1</u>', '<img src="$1">', '<a href="$1">$2</a>', '<font style="color:$1;font-size:$2;font-face:$3">$4</font>', ); return preg_replace($search,$replace,$input); } Now when i use this command($komentar is variable which is loaded from mysql): echo bbcode($komentar); it shows me this error: Warning: preg_replace() [function.preg-replace]: Unknown modifier '/' in /home/dotars/public_html/includes/functions.php on line 212 What is wrong here ? :S Similar TutorialsHi I'm trying to get rid of the deprecated functions like the eregi replace, but is currently experiancing a few problems.
I have the following function
function filterBadWords($str) { $result1 = mysql_query("SELECT word FROM StringyChat_WordBan") or die(mysql_error()); $replacements = "#~"; while($row = mysql_fetch_assoc($result1)) { $str = eregi_replace($row['word'], str_repeat('#~', strlen($row['word'])), $str); } return $str; }and tried changing it into function filterBadWords($str) { $result1 = mysql_query("SELECT word FROM StringyChat_WordBan") or die(mysql_error()); $replacements = "(G)"; while($row = mysql_fetch_assoc($result1)) { $str = preg_replace($row['word'], str_repeat('(G)', strlen($row['word'])), $str); } return $str; }but now I'm getting errors like preg_replace(): Delimiter must not be alphanumeric or backslash Any help regarding this? i get the following error in the comments in wordpress blog..: Warning: preg_replace() [function.preg-replace]: No ending delimiter '~' found in /home/doughhhh/public_html/wp-content/themes/upload/includes/theme-comments.php on line 75 the content of the file is: Code: [Select] <?php // Fist full of comments if (!function_exists("custom_comment")) { function custom_comment($comment, $args, $depth) { $GLOBALS['comment'] = $comment; ?> <li <?php comment_class(); ?>> <a name="comment-<?php comment_ID() ?>"></a> <div id="li-comment-<?php comment_ID() ?>" class="comment-container"> <div class="comment-head"> <?php if(get_comment_type() == "comment"){ ?> <div class="avatar"><?php the_commenter_avatar($args) ?></div> <?php } ?> <div class="reply"> <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?> </div><!-- /.reply --> </div><!-- /.comment-head --> <div class="comment-entry" id="comment-<?php comment_ID(); ?>"> <span class="arrow"></span> <div class="comment-info"> <div class="left"><span class="name"><?php the_commenter_link() ?></span></div> <div class="right"> <span class="date"><?php echo get_comment_date(get_option( 'date_format' )) ?> <?php _e('at', 'themejunkie'); ?> <?php echo get_comment_time(get_option( 'time_format' )); ?></span> <span class="perma"><a href="<?php echo get_comment_link(); ?>" title="<?php _e('Direct link to this comment', 'themejunkie'); ?>">#</a></span> <span class="edit"><?php edit_comment_link(__('Edit', 'themejunkie'), '', ''); ?></span> </div> <div class="clear"></div> </div> <?php comment_text() ?> <?php if ($comment->comment_approved == '0') { ?> <p class='unapproved'><?php _e('Your comment is awaiting moderation.', 'themejunkie'); ?></p> <?php } ?> </div><!-- /comment-entry --> </div><!-- /.comment-container --> <?php } } // PINGBACK / TRACKBACK OUTPUT if (!function_exists("list_pings")) { function list_pings($comment, $args, $depth) { $GLOBALS['comment'] = $comment; ?> <li id="comment-<?php comment_ID(); ?>"> <span class="author"><?php comment_author_link(); ?></span> - <span class="date"><?php echo get_comment_date(get_option( 'date_format' )) ?></span> <span class="pingcontent"><?php comment_text() ?></span> <?php } } if (!function_exists("the_commenter_link")) { function the_commenter_link() { $commenter = get_comment_author_link(); if ( preg_match( '~]* class=[^>]+>~', $commenter ) ) {$commenter = preg_replace( '~(]* class=[\'"]?)~', '\\1url ' , $commenter ); } else { $commenter = preg_replace( '~(<a )/', '\\1class="url "~' , $commenter );} echo $commenter ; } } if (!function_exists("the_commenter_avatar")) { function the_commenter_avatar($args) { $email = get_comment_author_email(); $avatar = str_replace( "class='avatar", "class='photo avatar", get_avatar( "$email", $args['avatar_size']) ); echo $avatar; } } ?> help! [attachment deleted by admin] Hey, I need help with my BBCode function... im trying to allow it to embed you tube videos but im kinda confused as to how i can do it when i need the link to be in the bbcode html ouput twice with my current method. Here is my function: http://www.paste.to/MzE3Mg== I could not post the script as it was messing up with this site's BBcode. Any one can help me please ? Thankss Hello guys, i need help with my BBcoding ... Its about browser game.. but i need someone who has some time to listen me about how things work and than help me.. every time i put : when sending message ingame i automaticly get smiley ... but please if anyone have time let me know Best Regards I'm trying to work on BBCode for a forum. I have this: Code: [Select] <?php $bb_Code = array( '[ code]' => '<code>', '[ /code]' => '</code>' ); foreach ($bb_Code as $value => $replace) { $text = str_replace($value, $replace, $text); } ?> Question is.. how would I get it so when I insert information into the database it gets cleaned (strip_tags, etc.) but doesn't disturb what is in the [ code ] tags? So basically it makes the html, etc. in the [ code ] tags just plain text? Thanks in advanced. 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? ok bare with me if this sounds stupid. How do i get it so that bbcode can be added within a textarea tag? im assuming it is a lot of str_replace. is there a tutorial on this somewhere? hi, i have the following which parses the quote tag and replaces it with a span $bb[] = "#\[quote\](.*?)\[/quote\]#si"; $html[] = "<span class='quote'>\\1</div>"; what i would like however is for the bbcode to be Quote from: xxxx where xxxx is the name of the person you are quoting. so how would i parse this? I also need the same for font color but if someone can provide a solution to this one then i think i could work it out. I was wonder how I could make some BBcode for my messaging system I made for a website. I made some simple ones like just by replace [ red ] with font color etc. I want to know how to do something like this: [ url = http://google . ca] Google [ / url ] without the spaces. ~AJ Hi there, This isn't really a bbcode parser problem, but it kind of is. And I am awful at explaining things, as you have noticed. The problem is that I'd like to allow input for the main page of a site. But the part has this structu Code: [Select] <div class='mainheader'>TITLE</div> <div class='content'> The content of the webpage blabla</div> I'd like this whole page to be changeable, so also the title and the content. But how can I ensure that people use the content div after the mainheader div? I would like it if it were possible that they only needed to 'select' the header, how can I do this? Thanks in advance! Code: [Select] $txt = preg_replace( "#\[yt\]http://www.youtube.com/watch?v=(.+?)\[/yt\]#is", '<embed src="http://www.youtube.com/v/\\1" type="application/x-shockwave-flash" wmode="transparent" width="512" height="313" allowfullscreen="true" />', $txt ); Okay this is my code just for inserting it into HTML, the problem is whenever I try to use: Code: [Select] [yt]http://www.youtube.com/watch?v=l1G7TJD6Xu0[/yt] On my form to enter it does not work? But if I use Code: [Select] $txt = preg_replace( "#\[yt\](.+?)\[/yt\]#is", '<embed src="http://www.youtube.com/v/\\1" type="application/x-shockwave-flash" wmode="transparent" width="512" height="313" allowfullscreen="true" />', $txt ); Code: [Select] [yt]l1G7TJD6Xu0[/yt] It works. But I want to use the full URL of the youtube, so I don't want my users to copy/paste just the "l1G7TJD6Xu0" Code , any help? Thank you Right now i used the following code to convert bbcode to html/php. i also need to incorporate QUOTE=name and URLS as well..how do i do that? Code: [Select] function bbcode_format ($str) { $str = htmlentities($str); $start = array( '/\[b\](.*?)\[\/b\]/is', '/\[i\](.*?)\[\/i\]/is', '/\[u\](.*?)\[\/u\]/is', '/\[a\=(.*?)\](.*?)\[\/a\]/is', '/\[color\=(.*?)\](.*?)\[\/color\]/is', '/\[img\](.*?)\[\/img\]/is', '/\[header\](.*?)\[\/header\]/is' ); $finish = array( '<strong>$1</strong>', '<em>$1</em>', '<u>$1</u>', '<a href="$1">$2</a>', '<span style="color: $1">$2</span>', '<img src="$1" />', '<span class="h3">$1</span>' ); $str = preg_replace ($start, $finish, $str); return $str; } This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=317329.0 I have this function: function bbcode($input){ $input = strip_tags($input); $input = htmlentities($input); $search = array( '/\[b\](.*?)\[\/b\]/is', '/\[i\](.*?)\[\/i\]/is', '/\[u\](.*?)\[\/u\]/is', '/\[img\](.*?)\[\/img\]/is', '/\[url=http://(.*?)\](.*?)\[\/url\]/is', '/\[color=(.*?)\](.*?)\[\/color\]/is', '/\[yt\](.*?)\[\/yt\]/is', ); $replace = array( '<b>$1</b>', '<i>$1</i>', '<u>$1</u>', '<img src="$1" style="border:0">', '<a href="$1">$2</a>', '<font style="color:$1">$2</font>', '<object width="660" height="405"> <param name="movie" value="'.str_replace("watch?v=", "v/", $1).'" /> <param name="allowFullScreen" value="true" /> <param name="allowscriptaccess" value="always" /><embed type="application/x-shockwave-flash" width="660" height="405" src="'.str_replace("watch?v=", "v/", $1).'" allowscriptaccess="always" allowfullscreen="true"></embed> </object>', ); return nl2br(preg_replace($search,$replace,$input)); } There is an error on code: <param name="movie" value="'.str_replace("watch?v=", "v/", $1).'" /> and on: <param name="allowscriptaccess" value="always" /><embed type="application/x-shockwave-flash" width="660" height="405" src="'.str_replace("watch?v=", "v/", $1).'" allowscriptaccess="always" allowfullscreen="true"> How can i use str_replace in this situation ? :S I'm trying to work on BBCode for a forum. Code: [Select] <?php $bb_Code = array( '[ code]' => '<code>', '[ /code]' => '</code>' ); foreach ($bb_Code as $value => $replace) { $text = str_replace($value, $replace, $text); } ?> Question is.. how would I get it so when I insert information into the database it gets cleaned (strip_tags, etc.) but doesn't disturb what is in the [ code ] tags? So basically it makes the html, etc. in the [ code ] tags just plain text? Thanks in advanced. This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=348145.0 Code: [Select] [b][i][u] test test test test test test test test test test test test test test test. [/b][/i]/[u] If I need to cut the first five words, the bbcode closing tags will be remove. i am wanting to add a code tag to my bbcode list. Anything within this tag will be printed as is(like the html code). I have tried using htmlspecialchars() but it isnt printing the code. Here is the parser section for the tag: $patern = "#\[code = ([^\]]*)\]([^\[]*)\[/code\]#i"; $replace = "<table class=\"code\"> <tr> <th>\\1</th> </tr> <tr> <td>".htmlspecialchars('\\2')."</td> </tr> </table>"; $str=preg_replace($patern, $replace, $str); Any ideas? This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=354638.0 I am trying to replace image code and resize it at the same time but gave me error. Warning: getimagesize($1): failed to open stream: No such file or directory in C:\wamp\www\ . . . . . public function resize_Images($originalImage,$toWidth,$toHeight){ // Get the original geometry and calculate scales list($width, $height) = getimagesize($originalImage); $xscale=$width/$toWidth; $yscale=$height/$toHeight; // Recalculate new size with default ratio if ($yscale>$xscale){ $new_width = round($width * (1/$yscale)); $new_height = round($height * (1/$yscale)); } else { $new_width = round($width * (1/$xscale)); $new_height = round($height * (1/$xscale)); } // Resize the original image $imageResized = imagecreatetruecolor($new_width, $new_height); $imageTmp = imagecreatefromjpeg ($originalImage); imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height); return $imageResized; } public function bhl_bbcodes($str) { $simple_search = array( '/\[img\](.*?)\[\/img\]/is', ); $simple_replace = array( '<img src="'.$this->resize_Images('$1', 700, 500).'" />', ); $str = preg_replace ($simple_search, $simple_replace, $str); return $str; } |