SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    highlight_string / preg_replace

    Okay, I did a search already and found a post by the good Dr.

    Basically I have a block of text, with some [ php][ /php] tags in it which I want "highlighted".

    Code:
    $body = preg_replace('#\[ php](.+?)\[/ php]#eis', 'my_highlight_string(\\'$1\\')', $body);
    is what it is, and I seem to be getting an error unexpected '$' - I thought maybe something wasn't escaped properly, but I've hardly used preg_replace so I'm a bit clueless as to what I'm looking for

    Thanks!

  2. #2
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using " around the function and ' around the matched result usually works for me:

    PHP Code:
    $body preg_replace('#\[ php](.+?)\[/ php]#eis'"my_highlight_string('\\1')"$body); 

  3. #3
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Chris.

    I presume you meant there to be a $ before the 1 up there? Unfortunatly I can't get it to work, no errors this time - but it just returns nothing
    I've spend all night trying to figure this out, it should be simple.. grrr

    <code><font color="#000000">
    </font>
    </code>

    is all it returns

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    UK
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can't you just do this:
    PHP Code:
    $body preg_replace('#\[ php](.+?)\[/ php]#eis''my_highlight_string($1)'$body); 

  5. #5
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As it is, that returns an error as well...

    but anyway, i'm just going by advice from another thread

    Quote Originally Posted by DR_LaRRY_PEpPeR
    ooops, i forgot that highlight_string() output the string rather than returning it in my above code.


    here's the changed version that should work correctly:

    Code:
    $txt = preg_replace('#\[phps](.+?)\[/phps]#eis', 'my_highlight_string(\\'$1\\')', $txt);
    
    function my_highlight_string($txt)
    {
    	// Fix those escaped single quotes
    	$txt = str_replace("\\'", "'", $txt);
    
    	ob_start();
    	highlight_string($txt);
    	$txt = ob_get_contents();
    	ob_end_clean();
    
    	return $txt;
    }
    is there anything ovbious I am missing with this?

  6. #6
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've had trouble with this (that code specifically) as well. The amount of escaping combined with when things actually get parsed gets so convoluted.

    I've been meaning to get a firm grip on how /e expressions work with preg_replace, but I have not yet. Maybe I'lll find time for that today.

    Christian probably did not intend have a $ before the 1 in his code example... \1 is the alternate syntax that predates $1.. they work in slightly different ways but are both supported.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    UK
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by platinum
    As it is, that returns an error as well...

    but anyway, i'm just going by advice from another thread



    is there anything ovbious I am missing with this?
    This works on my home server
    PHP Code:
    <?php
    $string 
    '[ php]mysql_query($sql)[/ php]';
    $body preg_replace('#\[ php](.+?)\[/ php]#eis''my_highlight_string($1)'$string);
    print 
    $body;
    ?>
    Have I misunderstood the question?

  8. #8
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    UK
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No it doesn't.
    I had left the spaces in the regexp between the php tags.

  9. #9
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [php]<?php

    $str = "[ php ]<?php echo 'Hello'; ?>[ /php ]";

    echo preg_replace( '#\
    PHP Code:
    (.+?)\[\/php]#eis', 'highlight_string (\'$1\', 1)', $str );

    ?> 
    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  10. #10
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got it working with that code Sean - what was Matt on about with messed up quotes though?

    also found out you need to stripslashes(); before doing it too (from form input)...

  11. #11
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okay, one last question
    How the <bleep> are you supposed to add code to the tags? I want to wrap it with <div class="code">....</div> - i tried doing it within the quotes but it returned an regex error :|

  12. #12
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, no then you would need to do some sort of your own function...

    in continue with Sean's code:
    [php]<?php

    $str = "[ php ]<?php echo 'Hello'; ?>[ /php ]";

    echo preg_replace( '#\
    PHP Code:
    (.+?)\[\/php]#eis', 'my_highlight_string (\'$1\')', $str ); 

    function my_highlight_string ($string) {
    return 
    '<div class="code">' highlight_string($stringtrue) . '</div>';
    }

    ?> 
    - website

  13. #13
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or just do it like this:

    [php]<?php

    $str = "[ php ]<?php echo 'Hello'; ?>[ /php ]";

    echo '<div class="code">'.preg_replace( '#\
    PHP Code:
    (.+?)\[\/php]#eis', 'highlight_string (\'$1\', 1)', $str ).'</div>';

    ?> 
    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  14. #14
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Inside the function you posted:

    PHP Code:
    function my_highlight_string($txt) { 
       
    // Fix those escaped single quotes 
       
    $txt str_replace("\\'""'"$txt); 
       
    $code '<div class="code">';
       
    ob_start(); 
       
    highlight_string($txt); 
       
    $txt ob_get_contents(); 
       
    ob_end_clean(); 
       
    $code .= $text '</div>';
       return 
    $code
    }

    echo 
    preg_replace(
       
    '#\[ php](.+?)\[ /php]#eis'
       
    "my_highlight_string('\\1')"
       
    $text
    ); 
    "my_highlight_string('\\1')" works

  15. #15
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    edit: heh that was fast - i'm going to use "websites" example I think

    thanks - I was hoping I could do it inline - ah well, these things are never easy Just a reminder of how much I hate regex

    Thanks website - you seem to have a good grasp of it from all the regex related threads I've seen you reply to

  16. #16
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tested with a piece of code that had quotes in it:

    <?
    $string =
    Warning: Unexpected character in input: '' (ASCII=92) state=1 in /home/xxxxxxx/functions.php on line 21
    "hello";
    ?>

    is what it returned, i'm using stripslashes beforehand, didn't seem to make any difference


    Is there a code snippit to do this that someone has or can link me to that actually works? It must be a fairly common thing to do I would have thought... ah well.

  17. #17
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    don't know exacly what is causing this but this should work
    PHP Code:
    function my_highlight_string ($string) { 
    return 
    '<div class="code">' highlight_string(stripslashes($string), true) . '</div>'

    - website


Bookmarks

Posting Permissions

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