Jump to content

Regexp to match php-blocks in a HTML-file


trace

Recommended Posts

I've been Googling for a regexp to match php-blocks in a html-file. But Googling for "php regexp html" doesn't yield very much useful... I still believe the regexp has been written at least once...

 

The idea being to strip out php-from a html-file, but store the stripped out blocks for later...

 

I would need something like /<\?php (.*?)\?>/. But I would like it to match all possible php-blocks, see http://www.php.net/manual/en/language.basic-syntax.phpmode.php

 

And it should also handle strings etc. which might contain the string "?>". I don't even know which ways a block of php can contain "?>" without it closing the php-block...

Link to comment
Share on other sites

<html><body>
<?php echo 'some string ?>';?>
<script language="php">echo $foobar;</script>
</body></html>

would result in

<html><body>
<?php ?>
<script language="php"></script>
</body></html>

And the php blocks would be returned in an array...

Link to comment
Share on other sites

'/(<\?(?:php)?.*?\?>)/i'

 

 

but ofcourse.. that would result in errors if for whatever reason you're trying to like

 

echo '?>';

 

in your php but thats rare I suppose, but you could use some sort of look ahead to be sure you get the very last one, or at the very least an atomic group.. good luck :D

 

oh and you're using php? use preg_match_all instead of preg_match for greedy searches

Link to comment
Share on other sites

oh and you're using php? use preg_match_all instead of preg_match for greedy searches

 

If by greedy you mean preg_match returns first match vs. preg_match_all returns every match it finds, then sure, you can call that a greedy search.  Though that might confuse people, considering that that term is used to describe quantifiers, and that is not the same kind of greedy...

 

OP:

Not really sure what you're overall goal here is, but if you're scraping pages, php will not be in the file unless it's somehow commented out, outside of php tags to begin with. 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.