PHP - 如何通过空格和 html 标记处的正则表达式将字符串拆分为数组?
PHP - How to split string into array by regular expression at whitespace and html tag?
这是一个示例字符串:
$string = '<strong>Lorem ipsum dolor</strong> sit <img src="test.png" /> amet <span class="test" style="color:red">consec<i>tet</i>uer</span>.';
我想将字符串拆分为数组,这样只要命中空格或命中 html 标记(忽略 html 标记内的空格),字符串就会被拆分。例如:
Array
(
[0] => <strong>
[1] => Lorem
[2] => ipsum
[3] => dolor
[4] => </strong>
[5] => sit
[6] => <img src="test.png" />
[7] => amet
[8] => <span class="test" style="color:red">
[9] => consec
[10] => <i>
[11] => tet
[12] => </i>
[13] => uer
[14] => </span>
[15] => .
)
但是我无法做到这一点。我使用 preg_split 来实现这个想法,但我认为我的正则表达式有误。以下是我尝试过的一些表达方式,但结果不是我想要的。
$chars = preg_split('/(<[^>]*[^\/]>)/i', $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
/* Results */
Array
(
[0] => <strong>
[1] => Lorem ipsum dolor
[2] => </strong>
[3] => sit <img src="test.png" /> amet
[4] => <span class="test" style="color:red">
[5] => consec
[6] => <i>
[7] => tet
[8] => </i>
[9] => uer
[10] => </span>
[11] => .
)
其他正则表达式的结果为:
$chars = preg_split('/\s+(?![^<>]*>)/x', $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
/* Results */
Array
(
[0] => <strong>Lorem
[1] => ipsum
[2] => dolor</strong>
[3] => sit
[4] => <img src="test.png" />
[5] => amet
[6] => <span class="test" style="color:red">consec<i>tet</i>uer</span>.
)
另一个表达式的结果是(非常接近):
$chars = preg_split('/\s*(<[^>]*>)/i', $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
/* Results */
Array
(
[0] => <strong>
[1] => Lorem ipsum dolor
[2] => </strong>
[3] => sit
[4] => <img src="test.png" />
[5] => amet
[6] => <span class="test" style="color:red">
[7] => consec
[8] => <i>
[9] => tet
[10] => </i>
[11] => uer
[12] => </span>
[13] => .
)
你离获得它不远了。但是您需要将 <[^>]*>
更改为更具体的正则表达式 <\/?\w+[^<>]*>
然后您需要为空格 |\s+
设置交替。您也不需要 i
标志:
preg_split('/(<\/?\w+[^<>]*>)|\s+/', $string, null, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE)
这是一个示例字符串:
$string = '<strong>Lorem ipsum dolor</strong> sit <img src="test.png" /> amet <span class="test" style="color:red">consec<i>tet</i>uer</span>.';
我想将字符串拆分为数组,这样只要命中空格或命中 html 标记(忽略 html 标记内的空格),字符串就会被拆分。例如:
Array
(
[0] => <strong>
[1] => Lorem
[2] => ipsum
[3] => dolor
[4] => </strong>
[5] => sit
[6] => <img src="test.png" />
[7] => amet
[8] => <span class="test" style="color:red">
[9] => consec
[10] => <i>
[11] => tet
[12] => </i>
[13] => uer
[14] => </span>
[15] => .
)
但是我无法做到这一点。我使用 preg_split 来实现这个想法,但我认为我的正则表达式有误。以下是我尝试过的一些表达方式,但结果不是我想要的。
$chars = preg_split('/(<[^>]*[^\/]>)/i', $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
/* Results */
Array
(
[0] => <strong>
[1] => Lorem ipsum dolor
[2] => </strong>
[3] => sit <img src="test.png" /> amet
[4] => <span class="test" style="color:red">
[5] => consec
[6] => <i>
[7] => tet
[8] => </i>
[9] => uer
[10] => </span>
[11] => .
)
其他正则表达式的结果为:
$chars = preg_split('/\s+(?![^<>]*>)/x', $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
/* Results */
Array
(
[0] => <strong>Lorem
[1] => ipsum
[2] => dolor</strong>
[3] => sit
[4] => <img src="test.png" />
[5] => amet
[6] => <span class="test" style="color:red">consec<i>tet</i>uer</span>.
)
另一个表达式的结果是(非常接近):
$chars = preg_split('/\s*(<[^>]*>)/i', $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
/* Results */
Array
(
[0] => <strong>
[1] => Lorem ipsum dolor
[2] => </strong>
[3] => sit
[4] => <img src="test.png" />
[5] => amet
[6] => <span class="test" style="color:red">
[7] => consec
[8] => <i>
[9] => tet
[10] => </i>
[11] => uer
[12] => </span>
[13] => .
)
你离获得它不远了。但是您需要将 <[^>]*>
更改为更具体的正则表达式 <\/?\w+[^<>]*>
然后您需要为空格 |\s+
设置交替。您也不需要 i
标志:
preg_split('/(<\/?\w+[^<>]*>)|\s+/', $string, null, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE)