[PHP]highlight_string error [Answered]

Results 1 to 7 of 7
  1. #1
    Infraction Baɴɴed holthelper is offline
    Grand MasterRank
    Apr 2008 Join Date
    1,765Posts

    [PHP]highlight_string error [Answered]

    just as the title says.

    im using it as a function in OOP and i keep getting an error. (not working the way its supposed to)

    code:
    PHP Code:
    <?php
    class bbcode {
        
        function 
    php_box($text) {
            
    $output "<div style=\"margin: 5px 20px 20px;\">";
            
    $output .= "<div style=\"margin-bottom: 2px;\">PHP:</div>";
            
    $output .= "<table cellspacing=\"0\" cellpadding=\"6\" border=\"0\" width=\"100%\"><tbody>";
            
    $output .= "<tr><td style=\"border: 1px inset ;\">".highlight_string($text)."</td></tr>";
            
    $output .= "</tbody></table>";
            
    $output .= "</div>";
            return 
    $output;
        }

        function 
    parse($text) {
            
    $text " " $text;
            if (!(
    strpos($text"[") && strpos($text"]"))) {
                return 
    $text;    
            }else{

                
    $text preg_replace("/\[php\](.+?)\[\/php\]/is"$this->php_box("\\1"), $text);
                
                return 
    $text;
            }    
        }
    }
    $bbcode = new bbcode;
    ?>
    display code:
    PHP Code:
    <?php
    include('./function/Forum_Class.php');

    $my_content "[ php]<?php echo 'Test.'; ?>[/ php]";

    echo 
    $bbcode->parse($my_content);

    ?>
    and the pic of the error that im getting:

    first part is the error.
    second part is what it should look like within the div.

    please help me figure it out and i hope its not a simple fix too ^_^.
    Last edited by holthelper; 28-10-09 at 05:39 AM. Reason: added the image


  2. #2
    Grand Master BBim is offline
    Grand MasterRank
    Sep 2008 Join Date
    127.0.0.1Location
    1,110Posts

    Re: [PHP]highlight_string error

    PHP Code:
    <?php
    class bbcode {
        
        function 
    php_box($text) {
            
    $text highlight_string($texttrue);
            
    $output "<div style=\"margin: 5px 20px 20px;\">";
            
    $output .= "<div style=\"margin-bottom: 2px;\">PHP:</div>";
            
    $output .= "<table cellspacing=\"0\" cellpadding=\"6\" border=\"0\" width=\"100%\"><tbody>";
            
    $output .= "<tr><td style=\"border: 1px inset ;\">" $text "</td></tr>";
            
    $output .= "</tbody></table>";
            
    $output .= "</div>";
            return 
    $output;
        }

        function 
    parse($text) {
            
    $text " " $text;
            if (!(
    strpos($text"[") && strpos($text"]"))) {
                return 
    $text;    
            }else{

                
    $text preg_replace("/\[php\](.+?)\[\/php\]/is""\\1"$text);
                
    $text $this->php_box($text);
                
                return 
    $text;
            }    
        }
    }
    ?>
    I changed line 8 and lines 20 and 21.
    highlight_string syntax:
    mixed highlight_string ( string $str [, bool $return = false ] )
    http://www.php.net/manual/en/functio...ght-string.php
    if you dont put return true, it will output instead of return the string.

    I think it would first run the function php_box using \1 as parameter, then with the return try to replace on the preg_replace, so separating them works :)
    Last edited by BBim; 26-10-09 at 04:47 AM.

  3. #3
    Infraction Baɴɴed holthelper is offline
    Grand MasterRank
    Apr 2008 Join Date
    1,765Posts

    Re: [PHP]highlight_string error

    ive tried
    PHP Code:
    highlight_string($text1
    and it worked but it didnt highlight

  4. #4
    :-) s-p-n is offline
    DeveloperRank
    Jun 2007 Join Date
    Next DoorLocation
    2,097Posts

    Re: [PHP]highlight_string error

    highlight_string goes directly to the output buffer. In other words, you can't echo it out because it does that for you. [Unless you set the output flag to true; In which case it should work as a string like BBim proposed. The only thing messing you up is the parse() function, which doesn't work.]

    Actually, you don't even need OOP for this, and you're not doing anything special, so it's better to just leave it as a function.

    What's the point of parse() anyway? Just to strip out the [php] BB code? Anyway, I couldn't figure it out so I just left that part out.

    It seems to work, though I can't figure why you have it in a <table> ?

    So I took that out as well.. Well made it a div instead,
    PHP Code:
    <?php
    function php_box($text
    {
        if(
    strlen($text))
        {
            echo 
    '<div style="margin: 5px 20px 20px;">';
            echo 
    '<div style="margin-bottom: 2px;">PHP:</div>';
            echo 
    '<div style="padding:4px;border:#000 1px inset">';
            
            
    highlight_string($text);
            
            echo 
    '</div></div>';
            
            return 
    true;
        } else return 
    false;
    }
    ?>
    Hopefully you can finish what you need to do from there ;)


    Oh, and no need to echo anything when you call the function ;)
    PHP Code:
    php_box($str); 

    Edit:

    If you want to treat is as a string, I did an update on the function. Now it goes both ways, (string/output buffer)

    All you need to do is leave it alone for output functionality, or add (.., true) to the function for it to return a string.
    PHP Code:
    <?php
    function php_box($text$as_string false
    {
        if(
    strlen($text))
        {
            if(
    $as_string)
            {
                
    $output '<div style="margin: 5px 20px 20px;">';
                
    $output .= '<div style="margin-bottom: 2px;">PHP:</div>';
                
    $output .= '<div style="padding:4px;border:#000 1px inset">';
                
                
    $output .= highlight_string($texttrue);
                
                
    $output .= '</div></div>';
                
                return 
    $output;
            } else {
                echo 
    '<div style="margin: 5px 20px 20px;">';
                echo 
    '<div style="margin-bottom: 2px;">PHP:</div>';
                echo 
    '<div style="padding:4px;border:#000 1px inset">';
                
                
    highlight_string($text);
                
                echo 
    '</div></div>';
                
                return 
    true;
            }
        } else return 
    false;
    }




    php_box($str); //regular output
    echo php_box($strtrue); //string output
    ?>
    The bottom two lines return identical, though you can do more with the last one if needed..
    Last edited by s-p-n; 27-10-09 at 05:18 PM.

  5. #5
    Infraction Baɴɴed holthelper is offline
    Grand MasterRank
    Apr 2008 Join Date
    1,765Posts

    Re: [PHP]highlight_string error

    basically im passing many forum bbcodes through 1 string and i wanted this function to be a part of it to complete the [code] and [php] pack.

    i have about 13 bbcodes pass through 1 string and they all work at the same time but when i use the [php] one, its the only one i get errors with.

  6. #6
    :-) s-p-n is offline
    DeveloperRank
    Jun 2007 Join Date
    Next DoorLocation
    2,097Posts

    Re: [PHP]highlight_string error

    Oh you don't need that function, you can use preg_replace like so:
    PHP Code:
    <?php
    $text 
    '[U]Here [I]is[/i] some [b]BB Code[/B][/U]... [ PhP]<?php echo "hello world."; ?>[/ pHp].'
    /*Leave out spaces by the {php} BB code, I put them for it to show on RZ..*/

    $text preg_replace('/\[php\](.*)\[\/php\]/Usie','php_box("\\1")',$text);
    $text preg_replace('/\[b\](.*)\[\/b\]/Usi','<strong>\\1</strong>',$text);
    $text preg_replace('/\[I\](.*)\[\/I\]/Usi','<span style="font-style:italic;">\\1</span>',$text);
    $text preg_replace('/\[u\](.*)\[\/u\]/Usi','<span style="text-decoration:underline">\\1</span>',$text);
    echo 
    $text;
    ?>
    Also, instead of the function I offered before, just use this one:
    PHP Code:
    function php_box($text
    {
        return 
    '<div style="margin: 5px 20px 20px;">
        <div style="margin-bottom: 2px;">PHP:</div>
        <div style="padding:4px;border:#000 1px inset">
        '
    .highlight_string($text,true).'
        </div>
        </div>'
    ;

    Attached Images Attached Images
    Last edited by s-p-n; 27-10-09 at 11:30 PM.

  7. #7
    Infraction Baɴɴed holthelper is offline
    Grand MasterRank
    Apr 2008 Join Date
    1,765Posts

    Re: [PHP]highlight_string error

    Quote Originally Posted by s-p-n View Post
    Oh you don't need that function, you can use preg_replace like so:
    PHP Code:
    <?php
    $text 
    '[U]Here [I]is[/i] some [b]BB Code[/B][/U]... [ PhP]<?php echo "hello world."; ?>[/ pHp].'
    /*Leave out spaces by the {php} BB code, I put them for it to show on RZ..*/

    $text preg_replace('/\[php\](.*)\[\/php\]/Usie','php_box("\\1")',$text);
    $text preg_replace('/\[b\](.*)\[\/b\]/Usi','<strong>\\1</strong>',$text);
    $text preg_replace('/\[I\](.*)\[\/I\]/Usi','<span style="font-style:italic;">\\1</span>',$text);
    $text preg_replace('/\[u\](.*)\[\/u\]/Usi','<span style="text-decoration:underline">\\1</span>',$text);
    echo 
    $text;
    ?>
    Also, instead of the function I offered before, just use this one:
    PHP Code:
    function php_box($text
    {
        return 
    '<div style="margin: 5px 20px 20px;">
        <div style="margin-bottom: 2px;">PHP:</div>
        <div style="padding:4px;border:#000 1px inset">
        '
    .highlight_string($text,true).'
        </div>
        </div>'
    ;

    LOL i thank thee.

    it worked the same still in OOP i just changed
    PHP Code:
    $text preg_replace("/\[php\](.+?)\[\/php\]/is"$this->php_box("\\1"), $text); 
    with your line:
    PHP Code:
    $text preg_replace('/\[php\](.*)\[\/php\]/Usie','$this->php_box("\\1")',$text); 
    EDIT:
    i wish there was the thank button still, because i would totally thank you lol.

    thread question answered //closing thread
    Last edited by holthelper; 28-10-09 at 05:07 AM.



Advertisement