【发布时间】:2016-12-22 12:24:33
【问题描述】:
我需要由“~(从 0 到 13 的数字)”分隔并以“~end”结尾的文本,数组的每个位置都应该有大括号之间的文本。有人有想法吗?
TEXT:(原文有湖文字,可能还有html)
~0
aaaaaa1
aaaaaaaaaa
~1
bbbbbbbbbb
sdf23
324 <br>
sdfs
~2
cccccccccc
~3
ddddddddddd
~13
eeeeeeeeeee
~14
fffffffffff
~end
结束数组:
Array
(
[0] => aaaaaa1
aaaaaaaaaa
[1] => bbbbbbbbbbb
sdf23
324 <br>
sdfs
[2] => cccccccccc
[3] => dddddddddd
.
.
.
.
[13] => eeeeeee
[14] => fffffff
)
我的 PHP 与正则表达式:(失败)
$texto = "
~0
123hola321
yyyyyyyyyyy
~1
rrrrrrrrrrrr
sdf23
324 <br>
sdfs
~2
cccccccccc
~3
ddddddddddd
~13
ddddddddddd
~14
ddddddddddd
~end ";
$regex = '/^~(\d{1,2}.\n)(.*?)/m';
echo $regex;
preg_replace($regex,$texto,$matches);
echo "<pre>";
print_r($matches);
echo "</pre>";
// ^~(\d{1,2}.\n)
// ~\d{1,2} (.*?)2$
//
// ^~\d{1,2}(.*?)end$
谢谢
【问题讨论】:
-
做
preg_split("/~[0-9]+/", $texto);
标签: php regex preg-match preg-match-all preg-grep