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 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2004
    Location
    California
    Posts
    96
    Thanks
    0
    Thanked 0 Times in 0 Posts

    A highlight_string code that works?

    Hey all... I'm having one heckuva time getting my php code like tags working:
    PHP Code:
    $output preg_replace('|\[div](.*?)\[/div]|ise',"\"<div class='div_code'>\".highlight_string('\\\\1',true).\"</div>\""$row['body']); 
    I can get it to work like so:

    PHP Code:
    <?php
    $body 
    =
    "
    Some non code.
    [div]
    <?
    if(true){
        echo \"Code!!\";
    }
    ?>
    [/div]
    And some more...<stuff>
    "
    ;

    $output preg_replace('|\[div](.*?)\[/div]|ise'"highlight_string(\"\\1\", true)"$body);
    echo 
    $body;
    echo 
    "<div>";
    echo 
    $output;
    echo 
    "</div>";
    ?>
    But I can't when I pull the code outta the DB. Any clean way of doing this?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Depends on what your error is.
    My guess is, you are looking at outputting some tagged references as well correct. Others like say <p>This is the greatest thing in the world</p> etc? If your doing stuff like that, and its working fin for what you put in manually, I'd say off hand it probably is relating to the way your quotation and double quotations are. Try using addslashes or stripslashes accordingly to see if it solves your problem.

    You have your $row['body'] in your first section, but not the lower, so I'm assuming thats for your sql output.
    But without seeing your problems/errors and more code / output, sorry I can only guess as to what the root problem is.

    Edit:
    You don't need the ? in the replacment pattern here: (.*?) that will allow it to have any number of characters any number of times. So you don't need to add a zero or one modifier.

  • #3
    New Coder
    Join Date
    Feb 2004
    Location
    California
    Posts
    96
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, finally got it.

    PHP Code:
    $output preg_replace('|\[div](.*)\[/div]|ise',"\"<div class='div_code'>\".highlight_string(stripslashes('\\1'),true).\"</div>\""$row['body']); 
    Just change the "div" to "php".

  • #4
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Here's something much better than the php highlighter: http://mandala.co.uk/syntax_highlighting/. You might want to change the colors to something that's better for the eyes.


  •  

    Posting Permissions

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