Preg_match 帮助查找计数
大家好 我有一个字符串,
<font size="+1"><b>Open Directory Sites</b></font> (1-20 of 10000)<p>
我需要得到 10000 作为答案..我如何使用 preg_match ???注意:这对于多次出现匹配非常重要,
提前致谢
HI all
i have a string
<font size="+1"><b>Open Directory Sites</b></font> (1-20 of 10000)<p>
i need to get 10000 as answer .. how i use preg_match ??? Note: this is implortant that the multiple occurance of match
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
至少对于这种特殊情况,您可以使用
'/\(\d+\-\d+ of (\d+)\)/'
作为模式
。它匹配这样的字符串
({one-or-more-digits}-{one-or-more-digits} of {one-or-more-digits})
并捕获最后一个{一个或多个数字}
到一个组中(此处添加{}
只是为了清楚起见..)。prints:
因此,您要查找的 10000 个可以通过
$matches[1]
访问。在评论后进行编辑:如果您多次出现
({一个或多个数字}-{一个或多个数字},共 {一个或多个数字})
,您可以使用preg_match_all
来捕获它们。我不确定数字本身在没有它们出现的上下文的情况下有多大用处,但您可以这样做:prints:
同样,您要查找的内容将在
$matches[1],只是这一次它将是一个包含 1 个或多个实际值的数组。
At least for this particular case, you could use
'/\(\d+\-\d+ of (\d+)\)/'
as thepattern
.It matches strings like this
({one-or-more-digits}-{one-or-more-digits} of {one-or-more-digits})
and captures the last{one-or-more-digits}
into a group ({}
s added just for clarity's sake here..).prints:
So, the 10000 you are looking for would be accessible at
$matches[1]
.Edit after your comment: If you have multiple occurrences of
({one-or-more-digits}-{one-or-more-digits} of {one-or-more-digits})
, you can usepreg_match_all
to catch them all. I'm not sure how useful the numbers themselves are without the context in which they occur, but here's how you could do it:prints:
Again, what you're looking for would be in
$matches[1]
, only this time it'll be an array containing 1 or more of the actual values.