2016-09-28 14 views
0

我想刪除#xyzbegin和#xyzend之間之間的所有東西都刪除PHP正則表達式匹配/兩個字

#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 

我正則表達式是:

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

我想這太:

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

兩者都不起作用。請幫忙。

+0

'(?<=#xyzbegin)(。*?)(?=#xyzend)'? – ndn

+0

即匹配並刪除文件 –

+0

中的所有其他內容如何?你能給我們一個例子文件嗎? – ndn

回答

0

您需要s修改,以配合多

<?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); 
+0

奇怪http://www.noupe.com/development/php-regular-expressions.html說m是多行,但工作。 –

+0

'm'多行如果使用'^ $','s'將匹配換行符和'''這是你的情況 – cske