找到一个不总是相同的字符串 [PHP]

Find a string that is not always the same [PHP]

我需要帮助在一个变量中找到一些不总是相同的东西,然后把它放在另一个变量中。 我知道我要查找的内容有 5 个斜杠,它以 steam://joingame/730/ 开头,在最后一个斜杠之后有 17 个数字。

编辑:它没有以斜杠结尾,这就是为什么我需要在第五个斜杠后数 17 个数字

假设您要查找的内容如下所示:

steam://joingame/730/11111111111111/

那么您可以使用 explode() 作为一个简单的解决方案:

$gameId = explode('/', 'steam://joingame/730/11111111111111/');

var_dump($gameId[4]);

或者您可以使用正则表达式作为更复杂的解决方案:

preg_match('|joingame/730/([0-9]+)|', 'steam://joingame/730/11111111111111/', $match);

var_dump($match[1]);

这会将字符串拆分为一个数组,然后 return 最后一个元素作为 game_id。多少斜杠并不重要。永远是return最后一个。

$str = 'steam://joingame/730';  
$arr = explode("/", $str) ;
$game_id = end($arr);

接着龙魂说的

我修改了那里的代码,使字符串看起来像

蒸汽://joingame/730/11111111111111

蒸汽://joingame/730/11111111111111/

$str = 'steam://joingame/730/11111111111111/';  
$rstr = strrev( $str ); // reverses the string so it is now like /1111111111...
if($rstr[0] == "/") // checks if now first (was last ) character is a /
{
    $nstr = substr($str, 0, -1); // if so it removes the / 
}
else
{
    $nstr = $str; // else it dont
}

$arr = explode("/", $nstr) ;
$game_id = end($arr);

感谢您的帮助,我已经找到了解决问题的方法。我要 post pastebin 上代码的未注释版本,因为我无法让代码在此处正常工作。

code