...

View Full Version : *help!*Cancel BBcode Tag



EZE
03-02-2007, 11:33 PM
I've been trying to make a bbcode parser for canceling out html and bbcode in between 2 [cancel] tags, so users can input bbcode and such without it disappearing and parsing when they don't want it too. For example,
[b]yo]/b] would so up as
[b]yo]/b] in plain text. I have this code and it the only thing that doesn't work is the cancel tags:

<?php
$send = $_POST['send'];
$text = $_POST['text'];
if(!$send){
echo '<form action="" method="post"><textarea name="text"/></textarea><input type="submit" name="send"/></form>';
}
elseif($send){
function cancel($var){
if(preg_match('/\[cancel\](.*?)\[\/cancel\]/is',$var,$matches)){
$var = preg_replace('/\[cancel\](.*?)\[\/cancel\]/is',htmlentities('$1'),$var);
$var = str_replace(array("[", "]"), array("[", "]"), $matches[1]);
}
return $var;
}
cancel($text);
$text = preg_replace('/\[b\](.*?)\[\/b\]/is','<b>$1</b>',$text);
echo $text;
}
?> Anybody know whats wrong? I just learned the preg_match function today, so I may have done something wrong there...:confused: :confused: :confused:(Note: the brackets in the str replace should be html replacements.)

printf
03-03-2007, 12:45 AM
You will have to use preg_replace_callback(), and do your htmlentities() in the callback function!


$var = preg_replace('/\[cancel\](.*?)\[\/cancel\]/is','html_convert',$var);

function html_convert ( $value )
{
return htmlentities ( $value[1] );
}

EZE
03-03-2007, 01:36 AM
I'm not exactly sure what you mean, but I changed the preg_replace to preg_replace_callback, and this came up when I submitted the form:

Warning: preg_replace_callback() requires argument 2, '$1', to be a valid callback in /home/content/e/z/e/ezevolk/html/tests/bbcode/googlevideotest.php on line 10
What exactly should I change? I looked at preg_replace_callback on php.net and it confused me. Thank you for helping me though :) :thumbsup: :D !

EZE
03-03-2007, 04:03 PM
I also tried the code you gave me, and it shows up as <b>test</b> when i submit the form. This is my code now:
<?php
$send = $_POST['send'];
$text = $_POST['text'];
if(!$send){
echo '<form action="" method="post"><textarea name="text"/></textarea><input type="submit" name="send"/></form>';
}
elseif($send){
function html_convert($value)
{
return htmlentities($value[1]);
}
$var = preg_replace('/\[cancel\](.*?)\[\/cancel\]/is','html_convert',$var);
$text = preg_replace('/\[b\](.*?)\[\/b\]/is','<b>$1</b>',$text);
echo $text;
}
?>

papa_face
03-03-2007, 07:01 PM
Why are you trying to write one yourself when you obviously cant do it?
Just use the one on wikipedia. http://en.wikipedia.org/wiki/BBCode or http://www.iceteks.com/articles.php/javascript2/3

EZE
03-04-2007, 12:54 AM
Because I can't always find tutorials for the tags I'm doing, like the one above. I am pretty new to php, but I am not completely clueless on everything. I already have all of the img, url, email, b, i, s, etc tags parsed, I just have some more complicated ones that I need help with. Do you have any good links for making a [php] bbcode tag? I know how to use highlight_string, but I don't know how to implement it into a form.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum