简体   繁体   English

PHP正则表达式匹配/删除两个单词之间的所有内容

[英]PHP regex to match/delete everything between two words

I want to delete everything between #xyzbegin and #xyzend 我想删除#xyzbegin和#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. 请帮忙。

You need s modifier to match multiline 您需要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 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM