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 12 of 12
  1. #1
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Making [php] tags, having problems...

    Ok, im trying to make some php tags like the ones on this forum... Im running into problems when I have more than one set of tags being used in a string, it matches the first [php] and the last [/php].

    I have this:

    Code:
    /\[php\]([^\[]*)\[\/php\]/is
    which will match blocks, works fine until there is a [ in the block, then it doesnt work well, and as its PHP code there is a good chance there will be [ needed.

    So is there a way I could do this, and not match a pattern:

    eg:

    [php] NO MATCH [php] BUT ALLOW ALL ELSE [/php]

    Thanks

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ehm... no. That is, if I understood your question correctly.

    Let me recapitulate: You want to grab PHP code within the [php] tags. Your current regex chokes when it finds square brackets, as they appear quite frequently in PHP. This problem we can solve:

    Code:
    /\[php\](.+?)\[\/php\]/is
    You just match anything that is contained in the php tags. However, you also want to prevent the example string at the end of your post to be matched completely. We can solve this too, and you have already in your first regex, but then.... problem number one is still there.

    If I'm right until here, I think you're stuck if you rely soley on regexes. Once you have nested tags or 'invalid' tag nesting, things get fuzzy. You could have a deeper look at the manual and at conditional subpatterns, but I didn't get them to work properly. I can't say if they would work at all, but I wouldn't be surprised if this route is a dead end too.

    If you can settle on not caring for invalid nesting of tags, take the regex above. I use it on my blog, and yet there haven't been any problems with PHP code that wasn't matched and highlighted.

    As an alternative, give this PEAR package a chance:
    http://pear.php.net/package/HTML_BBCodeParser

    I'm not sure if they implemented matching for PHP tags, but the rest of the code might give you some ideas how to build your own parser.
    De gustibus non est disputandum.

  • #3
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What I mean is:

    Code:
    Post...
    [php]
    <?php
    Php code goes here...
    ?>[/php]
    More of post...
    [php]
    <?php
    more php code
    ?>
    [/php]
    More of post
    This is not incorrect nesting of tags, but if you use the regexp you gave me, it matches from the first [php] to the last [/php]. (That does happen for you right?

    Trouble is, as this is not just going to be used by me, I need a way that it can parse this automatically. I wondered if I would have to look into another way other than regex, so I will have a go today.

    If anyone has anymore ideas, please do share them.

  • #4
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok I have come up with a little routine that seems to be quite efficient and also sorts out any problems such as [php] inside a [php] tag. (editied:. I just realised that this does handle unclosed php tags how I want it to, I just added a [/php] to my string and forgot I had added it .)

    Here is the code if anyone is interested:

    PHP Code:
    $chunks explode('[/[b][/b]php]'$string);
    for(
    $i=0;$i<count($chunks);$i++){
        
    $data $chunks[$i];
        
    $phpcode stristr($data'[[b][/b]php]');
        if(
    $phpcode != '' && ($i+!= count($chunks))){
            
    $phpcode preg_replace("/^(\\[php\\]([\r|\n]+?){2,})/is""[php[b][/b]]"$phpcode);
            
    $phpcode .= '[/[b][/b]php]';
        } 
        echo 
    '<p class="monospace">'.nl2br(htmlentities($phpcodeENT_QUOTES)).'</p>';

    This is still in testing hence why it just outputs values at the end.

    EDIT: Noticed I had a few lines of pointless code right after I posted it. i changed:

    [php]if(!preg_match("/^(\\[php\\]([\r|\n]+?){2,})/is", $phpcode)){
    $phpcode .= '[/php]';
    } else {
    $phpcode = preg_replace("/^(\\[php\\]([\r|\n]+?){2,})/is", "
    PHP Code:
    ", $phpcode);
        $phpcode .= '[/[b][/b]php]';

    to

    PHP Code:
    $phpcode preg_replace("/^(\\[php\\]([\r|\n]+?){2,})/is""[php[b][/b]]"$phpcode);
    $phpcode .= '[/[b][/b]php]'
    EDIT 2: Changed strstr() to stristr() becuase I dont want it to be case sensitive.

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by missing-score
    This is not incorrect nesting of tags, but if you use the regexp you gave me, it matches from the first [php] to the last [/php]. (That does happen for you right?
    No, it doesn't. I get each block section seperately. I did use this code:

    Code:
    $str = '
    Post...
    [php]
    <?php
    Php code goes here...
    ?>[/php]
    More of post...
    [php]
    <?php
    more php code
    ?>
    [/php]
    More of post	
    ';
    
    preg_match_all('/\[php\](.+?)\[\/php\]/is', $str, $matches);
    var_dump($matches[1]);
    and the result was

    Code:
    array(2) { [0]=>  string(34) " <?php Php code goes here... ?>" [1]=>  string(28) " <?php more php code ?> " }
    Just what I needed. I'm testing this on PHP 4.3.4 on Win2k, if that should matter.

    Edit: Damn PHP tags messing my post up....
    De gustibus non est disputandum.

  • #6
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well I am very annoyed .

    When I tested the code i forgot to add the ?, which produced what I said. I spent a decent ammount of time getting that to work for the sake of a single ?.

    Thanks mordred, This will be something I will never forget.

    Post 800

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hehe, everyone gets bitten by the greedy/ungreedy switch, aka the question mark right of a quantifier. Happened to me too some years ago...
    De gustibus non est disputandum.

  • #8
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well thanks, Im going to get a sample up and I'll post the link here when its done. I would appreciate anyone who could test it for any problems/bugs ect.

  • #9
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, the link is: http://www.freeipb.net/highlight/highlight.php

    Feel free to play about, and try methods that you might use to bypass security ect, and tell me if there are any problems. Thanks. (I will be testing it but theres bound to be something that could be done that im not aware of)

  • #10
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If I feed it this:

    Code:
    [php]
    <?php
    print('foobar');
    ?>
    [/php]
    it chokes and spits out an error:

    <?php
    print(
    Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/freeipb/public_html/highlight/highlight.php on line 26
    'foobar\');
    ?>
    De gustibus non est disputandum.

  • #11
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok thats sorted, It was to due with the fact that I have my php.ini set slightly different than on the server its hosted on... It should work now, so please carry on testing... Im just testing at the moment that there is no way of calling functions by using a clever combination of quotes.

  • #12
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have been working on it alot, and I have this: <http://www.freeipb.net/highlight/test.php>. Theres a few alterations, and you can see a full syntax list at the bottom of the page.. again, please test it and tell me if you have any problems. It seems to be working pretty well so far.


  •  

    Posting Permissions

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