заменить полный тег bbcode пустым не работает php - PullRequest
0 голосов
/ 03 мая 2020

Мне нужно заменить полный тег 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
 ";

regex замена

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

ОЖИДАЕМЫЙ ВЫХОД

 hello M M M M M hello

Я использую это регулярное выражение [MSG=\'(.*?)\'](.*?)[\/MSG] что с ним не так?

1 Ответ

0 голосов
/ 03 мая 2020

Попробуйте это регулярное выражение

или

$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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...