从 PHP 中的文本块中获取具有特定前缀的数字 [英] Getting a number when it has certain prefix from a block of text in 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屋!