从 PHP 中的文本块中获取具有特定前缀的数字 [英] Getting a number when it has certain prefix from a block of text in PHP

查看:28
本文介绍了从 PHP 中的文本块中获取具有特定前缀的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的段落,在其中的一个随机点出现了一个像这样的字符串,我想提取它:

I have a big paragraph and at a random point in it a string like this occurs which I'd like to extract:

PID: 45678437

'PID' 前和数字后有一个空格,数字的长度可以是 6-10 个字符.

There is a space before 'PID' and after the number, the number can be 6-10 characters in length.

我实际上只需要数字,但想检查它前面是否有 PID:因为段落中可能还有其他大数字.

I actually only need the number but would like to check it is preceded with PID: as there may be other large numbers in the paragraph.

:)

推荐答案

/ PID: (\d{6,10})/

这将匹配一个空格后跟 PID: 后跟一个空格后跟 6-10 位数字并捕获数字.

This will match a space followed by PID: followed by a space followed by 6-10 digits and capture the digits.

我鼓励您学习正则表达式.它们是一个强大的工具.

I would encourage you to learn Regular Expressions. They are a powerful tool.

这篇关于从 PHP 中的文本块中获取具有特定前缀的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆