一个简单的函数,用于从 php 中的字符串返回数字 [英] a simple function for return number from string in php

查看:60
本文介绍了一个简单的函数,用于从 php 中的字符串返回数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从这段代码中获取排名:

 $RankStr = 'var trafficstatsSnippet = "/site/trafficstats;pysa1bpbPOVl6Wm5d4Zv4nKXKdM%3D/aahoonet.com/?adult=&category=&rank=1234567";'

我使用此代码:

<块引用>

$NewPOS = strpos($RankStr, "rank=");$SRank = substr($RankStr, $NewPOS + 5, 10);回声 $SRank;

由于 RANK 代码变量从 (1 - 25,000,000) ,我通过在 rank= 的起始位置加上 5 个进一步索引后选择最多 10 个字符来使用上述代码.

所以这个函数返回

1234567";

然后我想从字符串中获取这个数字.尝试 preg_match_allregex 但由于不熟悉这些函数,我无法获得任何可用的响应.

请帮我解决这个问题,如果有更多解决方案请提供!

解决方案

您可以将 preg_match 用作:

if(preg_match('/rank=(\d+)/',$RankStr,$m)) {$rank = $m[1];}

代码实战

I want capture Rank from this code :

 $RankStr = 'var trafficstatsSnippet =  "/site/trafficstats;pysa1bpbPOVl6Wm5d4Zv4nKXKdM%3D
 /aahoonet.com/?adult=&category=&rank=1234567";'

I use this code :

$NewPOS = strpos($RankStr, "rank=");
$SRank = substr($RankStr, $NewPOS + 5, 10);
echo $SRank;

because of RANK code variable from (1 - 25,000,000) , i use above code by selecting maximum 10 charachters after starting position of rank= plus 5 further index.

so this function return

1234567"; 

and after that i want to grab this number from string. trying preg_match_all or regex but becasue of nofamiliarity with these functions i cant gain any usable response.

Please Help me for this problem, If there is more solution please provide them!

解决方案

You can use preg_match as:

if(preg_match('/rank=(\d+)/',$RankStr,$m)) {
 $rank = $m[1];
}

Code in Action

这篇关于一个简单的函数,用于从 php 中的字符串返回数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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