php - 如何通过正则表达式分割字符串并在输出中保留模式?

标签 php regex arrays string preg-split

我这里有一些代码:

$testString = "text23hello54stack90overflow34test";
$testArray = preg_split("/[0-9]{2}/Uim", $testString);
echo "<pre>".print_r($testArray)."</pre>";

执行这些命令后,我有一个包含以下内容的数组:

{text, hello, stack, overflow, test}

我想修改它,所以我得到:

{text, 23hello, 54stack, 90overflow, 34test}

我怎样才能实现这个目标?

最佳答案

怎么样:

$testString = "text23hello54stack90overflow34test";
$testArray = preg_split("/(?=[0-9]{2})/Uim", $testString);
echo print_r($testArray);

输出:

Array
(
    [0] => text
    [1] => 23hello
    [2] => 54stack
    [3] => 90overflow
    [4] => 34test
)

关于php - 如何通过正则表达式分割字符串并在输出中保留模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12779596/

相关文章:

PHP - Cron 作业在特定日期和时间运行

regex - 协助构建正则表达式

c# - C#中段落编号的正则表达式

php - 解析数组的最佳方式

php - PHP登录出现错误

java - 实现词法分析器

arrays - 返回 NULL 的空数组的 array_length()

javascript - 根据与另一个数组的最佳匹配对对象数组进行排序

c - 关于 char 数组和指向它们的指针,我错过了哪些语法

php - 如果在 html 表中选中复选框,那么我想在按下更新按钮时更新该行的值