SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot LORDGreg's Avatar
    Join Date
    May 2001
    Location
    Ptuj, Slovenia
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    replace part of var using regex and highlight_string

    i have a text saved in variable and part of that var has [CCODE] in it. that CCODE should split the text between CCODE tags in variable and use the php function highlight_string on it..

    here is how i wanted to do that:

    PHP Code:
    $patterns = array();
    $replacements = array();

    // code [ccode] to color code (highlight_string()<img src="images/smilies/wink.gif" border="0" alt="">
    $patterns[0] = "#[CCODE](.*?)[/CCODE]#si";
    $replacements[0] = "<hr>"highlight_string(\1) ."<hr>";

    $text "

    blah blah blah
      
      [CCODE]
      <html>
      <head></head>
      <body>
      
      </body>
      </html>
      [/CCODE]
      
     
      weasel

    "
    ;


    $text preg_replace($patterns$replacements$text);

    echo 
    $text
    but it just doesn't work..

    can someone please help me with that? please, please,..

    thank you very much all :P
    lordgreg, sloreactor.com
    http://www.sloreactor.com/

  2. #2
    epsilon transition cupid's Avatar
    Join Date
    Aug 2001
    Location
    Kent, Ohio
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think your regex got corrupted when you posted. Try using the code tags next time when posting regex.

    The main problem with the regex was that you needed to escape the metacharacters. Also, setting the patterns and replacements to arrays is fine, but when you're calling preg_replace, you're sending it a pointer, and not the values. Here's what I did and it seems to have worked:

    Code:
    // code [ccode] to color code (highlight_string()<img src="images/smilies/wink.gif" border="0" alt="">
    $patterns = "/\[CCODE\](.*?)\[\/CCODE\]/si";
    $replacements = "<hr>". highlight_string(1) ."<hr>";
    
    $text = "
    
    blah blah blah
      
      [CCODE]
      <html>
      <head></head>
      <body>
      
      </body>
      </html>
      [/CCODE]
      
     
      weasel
    
    ";
    
    
    $text = preg_replace($patterns, $replacements, $text);
    
    echo $text;

  3. #3
    SitePoint Zealot LORDGreg's Avatar
    Join Date
    May 2001
    Location
    Ptuj, Slovenia
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i tried your code and it doesn't work neither!

    --
    maybe the vbulletin -code- and -php- functions are doing something.. well doesn't matter.. what's wrong?

    parse error? backslash? or mail me the code -> lordgreg@email.si..

    thanks cupid :P
    lordgreg, sloreactor.com
    http://www.sloreactor.com/

  4. #4
    epsilon transition cupid's Avatar
    Join Date
    Aug 2001
    Location
    Kent, Ohio
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry, I misread your question. I didn't realize that you were trying to highlight the stuff inside [ccode][/ccode]

    Try this:

    Code:
    // code [ccode] to color code (highlight_string()<img src="images/smilies/wink.gif" border="0" alt="">
    
    $text = "
    
    blah blah blah
    
      [CCODE]
      <html>
      <head></head>
      <body>
    
      </body>
      </html>
      [/CCODE]
    
    
      weasel
    ";
    
    
    
    if (preg_match("/\[CCODE\](.*?)\[\/CCODE\]/si", $text, $result))
           highlight_string(preg_replace("/\[(|\/)CCODE\]/i","",$text));
    else echo $text;
    Last edited by cupid; Nov 14, 2001 at 22:28.

  5. #5
    SitePoint Zealot LORDGreg's Avatar
    Join Date
    May 2001
    Location
    Ptuj, Slovenia
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that's great, but what about that text outside [CCODE]? i want that text also to apear! just like by vbulletin..

    and how to color the text inside [CCODE]?
    Last edited by LORDGreg; Nov 14, 2001 at 21:29.
    lordgreg, sloreactor.com
    http://www.sloreactor.com/

  6. #6
    epsilon transition cupid's Avatar
    Join Date
    Aug 2001
    Location
    Kent, Ohio
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hoo boy! I guess I got a little disoriented after all that regex

    I threw in so much redundant/unneeded code in there. Check out the edited code. The color comes in when you have php code in there

  7. #7
    SitePoint Zealot LORDGreg's Avatar
    Join Date
    May 2001
    Location
    Ptuj, Slovenia
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    some problems:

    1. the code snippet you wrote works like this:
    - if found any [ccode] tags in the text MAKE ALL $text highlight_string().
    - else just echo the whole $text.

    2. i wanted to be like this:

    -input:
    Code:
    $text = "
      bla bla
    
      and here also bla
    
      [ccode]
      $here = "something";
      <pre>harhar</pre>
      <b>the bold text - source</b>
      [/ccode]
    
      and again some text outside the ccode tag
    ";
    -output:
    ---------------------------------
    bla bla

    and here also bla

    Code:
      $here = "something";
      <pre>harhar</pre>
      <b>the bold text - source</b>
    and again some text outside the ccode tag
    ---------------------------------

    that means only what's inside ccode tag, goes through highlight_string...

    please please help
    Last edited by LORDGreg; Nov 15, 2001 at 02:58.
    lordgreg, sloreactor.com
    http://www.sloreactor.com/


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
  •