如何预分割所有字符,但不要分割<b>和<br/>

时间:2019-03-24 04:12:45

标签: php regex pcre

这里有关于[preg_split]的大量问题,但都与我的问题无关。我正在使用以下代码在PHP中将字符串拆分为字符,例如:

$str = "My <b>table</b> in brown <br> Help";
$char = preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($char);

输出为:

Array
(
    [0] => M
    [1] => y
    [2] =>  
    [3] => <
    [4] => b
    [5] => >
    [6] => t
    [7] => a
    [8] => b
    [9] => l
    [10] => e
    [11] => <
    [12] => /
    [13] => b
    [14] => >
    [15] =>  
    [16] => i
    [17] => n
    [18] =>  
    [19] => b
    [20] => r
    [21] => o
    [22] => w
    [23] => n
    [24] =>  
    [25] => <
    [26] => b
    [27] => r
    [28] => >
    [29] => ...
)

但是我期望以下几点:

Array
(
    [0] => M
    [1] => y
    [2] =>  
    [3] => <b>
    [6] => t
    [7] => a
    [8] => b
    [9] => l
    [10] => e
    [11] => </b>
    [15] =>  
    [16] => i
    [17] => n
    [18] =>  
    [19] => b
    [20] => r
    [21] => o
    [22] => w
    [23] => n
    [24] =>  
    [25] => <br>
    [29] => ...
)

例如<b>,</b>,<br>,<i>,</i>之类的字符不应拆分。

谢谢。

1 个答案:

答案 0 :(得分:1)

您可以通过使用PREG_SPLIT_DELIM_CAPTURE选项捕获每个值来分割<>中的单个字符或一系列字符来实现此目的:

$str = "My <b>table</b> in brown <br> Help";
$char = preg_split('#(</?[a-z]+>|[a-z ])#ui', $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
print_r($char);

输出:

Array (
  [0] => M
  [1] => y
  [2] =>
  [3] => <b>
  [4] => t
  [5] => a
  [6] => b
  [7] => l
  [8] => e
  [9] => </b>
  [10] =>
  [11] => i
  [12] => n
  [13] => 
  [14] => b
  [15] => r
  [16] => o
  [17] => w
  [18] => n
  [19] =>
  [20] => <br>
  [21] =>
  [22] => H
  [23] => e
  [24] => l
  [25] => p 
)

Demo on 3v4l.org