用空的不起作用的php替换完整的bbcode标记

问题描述 投票:0回答:1

我需要将一个完整的bbcode标记替换为一个空格(在此示例中为一个“ M”)

这是到目前为止的我的php代码

$string ="
 hello
 [MSG='user1, comment: 253434, userid: 1232'] TEXT1[/MSG]

   [MSG='user2, comment: 343425, userid: 4231'] 
   TEXT2
   [/MSG]

   [MSG='user3, comment: 234345, userid: 1423']
   TEXT3
  [/MSG]
   [MSG='user4, comment: 253434, userid: 123242']
   TEXT4
  text 4
  text 4
  [/MSG]

  [MSG='user5, comment: 251234, userid: 1652'] TEXT5[/MSG]
 hello
 ";

正则表达式替换

 $string = preg_replace("[MSG=\'(.*?)\'](.*?)[\/MSG]", "M", $string);

期望的输出

 hello M M M M M hello

我正在使用此正则表达式[MSG=\'(.*?)\'](.*?)[\/MSG]怎么了?

php
1个回答
0
投票

尝试此regex

$re = '/(\[MSG=.*?\[\/MSG\]\s+)/ms';
$str = '$string ="
 hello
 [MSG=\'user1, comment: 253434, userid: 1232\'] TEXT1[/MSG]

   [MSG=\'user2, comment: 343425, userid: 4231\'] 
   TEXT2
   [/MSG]

   [MSG=\'user3, comment: 234345, userid: 1423\']
   TEXT3
  [/MSG]
   [MSG=\'user4, comment: 253434, userid: 123242\']
   TEXT4
  text 4
  text 4
  [/MSG]

  [MSG=\'user5, comment: 251234, userid: 1652\'] TEXT5[/MSG]
 hello';
$subst = ' M';

$result = preg_replace($re, $subst, $str);

echo "The result of the substitution is ".$result;
 hello M M M M Mhello
© www.soinside.com 2019 - 2024. All rights reserved.