在第一次出现标识符后获取字符串中的第一个数字

编程入门 行业动态 更新时间:2024-10-23 06:23:07
本文介绍了在第一次出现标识符后获取字符串中的第一个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在研究一个获取这样的字符串的函数:

I'm working on a function that gets a string like this:

identifier 20 j. - cat: text text text aaaa dddd ..... cccc 60' - text, 2008

并提取数字 20 所以字符串中第一次出现 identifier 之后的第一个数字(包括空格)

and extracts the number 20 so the first number in the string right after the first occurrence of identifier (including whitespace)

但是如果我有这样的字符串:

But if I had a string like this:

identifier j. - cat: text text text aaaa dddd ..... cccc 60' - text, 2008

函数应该返回NULL,因为identifier(包括空格)出现后没有数字

the function should return NULL because there is no number right after the occurrence of identifier (including whitespace)

你能帮我吗?谢谢

推荐答案

您可以为此使用正则表达式:

You can use a regular expression for this:

$matches = array(); preg_match('/identifier\s*(\d+)/', $string, $matches); var_dump($matches);

\s* 是空格.(\d+) 匹配一个数字.

\s* is whitespace. (\d+) matches a number.

您可以将其包装在一个函数中:

You can wrap it in a function:

function matchIdentifier($string) { $matches = array(); if (!preg_match('/identifier\s*(\d+)/', $string, $matches)) { return null; } return $matches[1]; }

更多推荐

在第一次出现标识符后获取字符串中的第一个数字

本文发布于:2023-10-27 18:52:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534241.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标识符   字符串   数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!