a simple function for return number from string in php

痞子三分冷 提交于 2019-12-01 08:20:44

问题


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!


回答1:


You can use preg_match as:

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

Code in Action



来源:https://stackoverflow.com/questions/4175941/a-simple-function-for-return-number-from-string-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!