PHP从文本中的字符串中提取子字符串

时间:2014-08-28 13:26:14

标签: php regex text substring text-extraction

我有一个由许多字符串和每个字符串组成的文本,如果检查结果为true,我想得到一个具有某些属性的特定字符串:

  1. 字符串以" atm"
  2. 之类的符号开头
  3. 在符号后面有一个带有变量lenght的数字部分
  4. 即。这个词可能像atm123456或atm7890

    感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

你能试试吗

    $thestring='atm123456';
    $thestrToEx = explode(' ',$thestring );
    $thestrToExArr=array_walk($thestrToEx,'intval');
    $theValues=explode($thestrToExArr,$thestring);
    echo $thestrToExArr.$theValues[1];

答案 1 :(得分:1)

您可以使用正则表达式和preg_match()函数

$string = "atm123456";
$pattern = "(atm\d+)";
preg_match($pattern, $string, $matches); // you may use preg_match_all() as well
print_r($matches);

输出:

Array
(
    [0] => atm123456
)

PHP demo | Regex demo

答案 2 :(得分:1)

如果是多个值或重复的项目。你可以像这样使用preg_match_all

$string = "atm123456 with atm7890 items";
$pattern = "(atm\d+)";
preg_match_all($pattern, $string, $matches);
print_r($matches);
相关问题