Srikanth Koneru Srikanth Koneru - 2 months ago 6
PHP Question

PHP regex to match/delete everything between two words

I want to delete everything between #xyzbegin and #xyzend

#xyzbegin
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{QUERY_STRING} !.*=.*
RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress_logged_in|wp-postpass_).*$
RewriteCond %{DOCUMENT_ROOT}/wp-content/xyz-cache/$1/index.html -f
RewriteRule ^(.*) "/wp-content/xyz-cache/$1/index.html" [L]

</IfModule>
#xyzend

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress


my regex is :

'/#xyzbegin(.*)#xyzend/m'


I tried this too :

'/^#xyzbegin(.*)#xyzend$/m'


both doesn't work. Please help.

Answer

You need s modifier to match multiline

<?php
/**
*/

$in = <<<CODE

#xyzbegin
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{QUERY_STRING} !.*=.*
RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress_logged_in|wp-postpass_).*$
RewriteCond %{DOCUMENT_ROOT}/wp-content/xyz-cache/$1/index.html -f
RewriteRule ^(.*) "/wp-content/xyz-cache/$1/index.html" [L]

</IfModule>
#xyzend

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress";
CODE;



echo preg_replace('/#xyzbegin(.*)#xyzend/s','',$in );
Comments