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 13 of 13
  1. #1
    Regular Coder
    Join Date
    Feb 2006
    Location
    The Netherlands
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Highlighting code found with PCRE

    Hey, I'd like to be able to insert code on my site using [ code] and [/code] tags. Everything within those tags should be put withing <span class="code"> and </span>, and I want to use highlight_string() to highlight it. I would like to do this using PCRE, how would I set about doing it? I'm capable of reading PCRE (and if I'm not, I'll study it some more) so if you know the solution, posting the answer code is enough. This has been frustrating me since a long while, so help would be greatly appreciated.

    Thanks in advance.
    Yeah that.

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Vin0rz
    ...if you know the solution, posting the answer code is enough. ...
    this is not a free coding service.
    maybe show us how far you got and then someone cann fill in the blanks...
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    Regular Coder
    Join Date
    Feb 2006
    Location
    The Netherlands
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by raf
    this is not a free coding service.
    maybe show us how far you got and then someone cann fill in the blanks...
    Oh, sure, didn't mean that :P

    Anyway, how I tried to do it was using preg_split to filter out everything which was between [ code] and [/code], apply highlight_string(), then use str_replace() to replace the original [code ]code[/code] with the <span class="code">highlighted code</span>, but that only removed the code.

    As it totally didn't work out, I decided not to post it because I thought that if everyone would start off from where I failed you wouldn't get anywhere. Also, I think this is a very inefficient way of doing this and I supposed there would be a way designed especially for cases like this.
    Yeah that.

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    just post the relevant code.

    there are enough people around here that are experienced enough to read your code and see what you try to do + most of them are assertive enough to tell you you're doing it completely wrong (if that would turn out to be the case)
    if you would also include a sample of possible input and expected output, then we'll have all we need...
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #5
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    It took me a long time to figure out how to achieve it. And it's not as easy as you'd think it should be. Also, it's a potential security risk if you do not properly strip appropriatet chars.

    You'll need to do an evaluation on the backreference. Also you will need to use output buffering.
    Omnis mico antequam dominus Spookster!

  • #6
    Regular Coder
    Join Date
    Feb 2006
    Location
    The Netherlands
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by raf
    just post the relevant code.

    there are enough people around here that are experienced enough to read your code and see what you try to do + most of them are assertive enough to tell you you're doing it completely wrong (if that would turn out to be the case)
    if you would also include a sample of possible input and expected output, then we'll have all we need...
    PHP Code:
        $value preg_split($pattern$string);
        foreach(
    $value as $content){
         
    $newContent highlight_string($content);
         
    str_replace($content$newContent$string);
        }
       
      
    $pattern '[' preg_quote('[code]') . '(.*)' preg_quote('[/code']) . ']iU';

       
    $newString preg_replace($pattern'<span class="code">$1</span>'$string); 
    I'm not too good at writing PCRE yet, so there may be some errors here, but I tried a lot of variations.

    Edit: You know, this forums has an example of what I want: the [PHP] BBCode :P

    Quote Originally Posted by Mhtml
    It took me a long time to figure out how to achieve it. And it's not as easy as you'd think it should be. Also, it's a potential security risk if you do not properly strip appropriatet chars.

    You'll need to do an evaluation on the backreference. Also you will need to use output buffering.
    I already use output buffering, so I wouldn't mind that.
    Yeah that.

  • #7
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    What does output buffering have to do with this?
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #8
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Actually, maybe it wasn't syntax highlighting that had me working around the clock.. It was some sort of bb code I remember that much lol. And the buffering had something to do with PHP <= 4.2.0 ...

    PHP Code:
    function highlightstr($arr){
      return 
    highlight_string($arr[1],true);
    }
    $code preg_replace_callback("/\[php\]((.*))\[\\php\]/is","highlightstr",$str); 
    [edit:]Actually I think I used the buffering for some sort of tokenizing I had going on with my forum software.
    Last edited by Mhtml; 02-10-2006 at 05:22 PM.
    Omnis mico antequam dominus Spookster!

  • #9
    Regular Coder
    Join Date
    Feb 2006
    Location
    The Netherlands
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Mhtml
    Actually, maybe it wasn't syntax highlighting that had me working around the clock.. It was some sort of bb code I remember that much lol. And the buffering had something to do with PHP <= 4.2.0 ...

    PHP Code:
    function highlightstr($arr){
      return 
    highlight_string($arr[1],true);
    }
    $code preg_replace_callback("/\[php\]((.*))\[\\php\]/is","highlightstr",$str); 
    Looks good, will try after I've cleared up my RAM :P

    I'll post the results, thanks!
    Yeah that.

  • #10
    Regular Coder
    Join Date
    Feb 2006
    Location
    The Netherlands
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK, I tried it but I can't post the results because I'm having another problem... Bummer.
    Yeah that.

  • #11
    Regular Coder
    Join Date
    Feb 2006
    Location
    The Netherlands
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Mhtml
    Actually, maybe it wasn't syntax highlighting that had me working around the clock.. It was some sort of bb code I remember that much lol. And the buffering had something to do with PHP <= 4.2.0 ...

    PHP Code:
    function highlightstr($arr){
      return 
    highlight_string($arr[1],true);
    }
    $code preg_replace_callback("/\[php\]((.*))\[\\php\]/is","highlightstr",$str); 
    [edit:]Actually I think I used the buffering for some sort of tokenizing I had going on with my forum software.
    OK... One way or another that didn't work. I did it exactly the same way you did it...

    PHP Code:
    function highlightstr ($arr){
         return 
    highlight_string($arr[1], true);
        }
        
    $newString preg_replace_callback($pattern'highlightstr'$string); 
    Yeah that.

  • #12
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Worked fine for me.. What PHP version?
    Omnis mico antequam dominus Spookster!

  • #13
    Regular Coder
    Join Date
    Feb 2006
    Location
    The Netherlands
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Mhtml
    Worked fine for me.. What PHP version?

    Five.
    Yeah that.


  •  

    Posting Permissions

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