regex 获取字符串中数字之后出现的文本

6bc51xsx  于 2023-05-01  发布在  其他
关注(0)|答案(3)|浏览(142)

我有:

$string = "Some minimal or large text 820 some minimal or large descr";

我需要:

some minimal or large descr
b1zrtrql

b1zrtrql1#

str_pos()怎么样?

<?php
  $string = "Some minimal or large text 820 some minimal or large descr";
  echo substr($string, strpos($string, '820') + 4);
rwqw0loc

rwqw0loc2#

函数preg_split可以为你做!
它将字符串分割成一个数组,变换它。.
http://www.php.net/manual/en/function.preg-split.php

$array = preg_split("/[0-9]+/", "Some minimal or large text 820 some minimal or large descr");
print_r($array);
ymdaylpp

ymdaylpp3#

要隔离820后面的空格后面的some minimal or large descr,请匹配从字符串开头到一个或多个数字的所有非数字字符,然后是零个或多个空格。将这些匹配的字符替换为空字符串。
代码:(Demo

$string = "Some minimal or large text 820 some minimal or large descr";

var_export(
    preg_replace('/\D*\d+ */', '', $string)
);

如果可能出现多个数字,而您需要最后出现的数字之后的子字符串,则在匹配数字之前贪婪地匹配任何字符。(Demo

var_export(
    preg_replace('/.*\d+ */', '', $string)
);

相关问题