...

View Full Version : Making [php] tags, having problems...



missing-score
03-19-2004, 12:29 AM
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
and the last .

I have this:


/\
([^\[]*)\[\/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

Thanks

mordred
03-19-2004, 01:10 AM
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:



/\[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.

missing-score
03-19-2004, 10:26 AM
What I mean is:


Post...


<?php
Php code goes here...
?>
More of post...


<?php
more php code
?>

More of post

This is not incorrect nesting of tags, but if you use the regexp you gave me, it matches from the first
to the last . (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.

missing-score
03-19-2004, 01:32 PM
Ok I have come up with a little routine that seems to be quite efficient and also sorts out any problems such as
inside a
tag. (editied:. I just realised that this does handle unclosed php tags how I want it to, I just added a to my string and forgot I had added it :p.)

Here is the code if anyone is interested:


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


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

to


$phpcode = preg_replace("/^(\\
([\r|\n]+?){2,})/is", "[php]", $phpcode);
$phpcode .= '';

EDIT 2: Changed strstr() to stristr() becuase I dont want it to be case sensitive.

mordred
03-19-2004, 02:05 PM
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
to the last . (That does happen for you right?

No, it doesn't. I get each block section seperately. I did use this code:



$str = '
Post...


<?php
Php code goes here...
?>
More of post...


<?php
more php code
?>

More of post
';

preg_match_all('/\[php\](.+?)\[\/php\]/is', $str, $matches);
var_dump($matches[1]);


and the result was



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....

missing-score
03-19-2004, 02:16 PM
Well I am very annoyed :p.

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. :D

Post 800 :D

mordred
03-19-2004, 02:25 PM
Hehe, everyone gets bitten by the greedy/ungreedy switch, aka the question mark right of a quantifier. Happened to me too some years ago... :D

missing-score
03-19-2004, 02:32 PM
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.

missing-score
03-19-2004, 02:52 PM
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)

mordred
03-19-2004, 03:00 PM
If I feed it this:





<?php
print('foobar');
?>



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\');
?>

missing-score
03-19-2004, 03:23 PM
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.

missing-score
03-20-2004, 03:48 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum