Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    EZE
    EZE is offline
    New Coder
    Join Date
    Jan 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    *help!*Cancel BBcode Tag

    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,
    Code:
    [b]yo]/b]
    would so up as
    Code:
    [b]yo]/b]
    in plain text. I have this code and it the only thing that doesn't work is the cancel tags:
    PHP Code:
    <?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... (Note: the brackets in the str replace should be html replacements.)
    Last edited by EZE; 03-02-2007 at 11:40 PM.

  • #2
    New Coder
    Join Date
    Sep 2006
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You will have to use preg_replace_callback(), and do your htmlentities() in the callback function!

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

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


  • #3
    EZE
    EZE is offline
    New Coder
    Join Date
    Jan 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 !

  • #4
    EZE
    EZE is offline
    New Coder
    Join Date
    Jan 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I also tried the code you gave me, and it shows up as [cancel]<b>test</b>[/cancel] when i submit the form. This is my code now:
    PHP Code:
    <?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;
    }
    ?>

  • #5
    New Coder
    Join Date
    Jan 2007
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    EZE
    EZE is offline
    New Coder
    Join Date
    Jan 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •