如何在 delimeter 中拆分和忽略字符

  • 本文关键字:字符 拆分 delimeter php
  • 更新时间 :
  • 英文 :


假设要拆分$text是:

Question 1 - Parabolas
... /**/
Question 2 - Permutations
... /**/
Question 3 - Integration

如何分裂以获得分量计

"问题X-",其中X可以表示任何数字。通过:

$question = explode("Question X -", $text);

我绝对必须包括后面的破折号。

为此,您必须使用正则表达式。

例:

$text = "Question 1 - Parabolas";
preg_match('/Question [0-9]+ - (.*)/', $text, $matches);
echo $matches[1];

输出:

Parabolas

如果要使用多行执行此操作,则必须preg_match_all然后循环遍历匹配项。问题将在索引 1 处!

最新更新