douzong2206 2016-09-28 06:34
浏览 81
已采纳

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

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.

  • 写回答

1条回答 默认 最新

  • douwendu2460 2016-09-28 06:47
    关注

    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 );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了