-2

I need help finding something in a variable that isn't always the same, and then put it in another variable. I know that what I'm looking for has 5 slashes, it starts with steam://joingame/730/ and after the last slash there are 17 numbers.

Edit: It doesn't end with a slash, thats why I need to count 17 numbers after the fifth slash

Bob123
  • 1
  • 3

4 Answers4

0

Assuming what you're looking for looks something like this:

steam://joingame/730/11111111111111/

Then you could use explode() as a simple solution:

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

var_dump($gameId[4]);

or you could use a regex as a more complex solution:

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

var_dump($match[1]);
Dragony
  • 1,712
  • 11
  • 20
  • I've got a website's code in a variable and somewhere in that variable it says steam://joingame/730/1234567/RANDOMNUMBERS RANDOMNUMBERS is always seventeen numbers, so seventeen numbers after the fifth slash should be the end of the new variable. It doesn't end with a slash, thats why we need to count 17 numbers after the last slash. – Bob123 Sep 03 '16 at 21:50
  • @Dragony why did you used [0-9]+ to match the digits in regex? I suggest to use `\d{17}` , also i think he wants the entire link in the matching group, and you need to escape the `/` with `\/`. And here regex is the best solution, and is not that complex. Your answer is the best so far, so nice job :) – M. I. Sep 03 '16 at 23:12
  • I use [0-9]+ more out of habbit, less to remember ;) I don't need to escape "/" if I use pipes "|" as a delimiter for the regex. – Dragony Sep 04 '16 at 08:08
  • @Bob123 if you've got the whole website as a variable, then the regex is the way to go. – Dragony Sep 04 '16 at 08:09
0

This splits the string into an array then return the last element as the game_id. It doesn't matter how many slashes. It will always return the last one.

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

Following on from what DragonSpirit said

I modified there code so the string can look like

steam://joingame/730/11111111111111

or

steam://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);
R1CH101
  • 60
  • 8
0

Thanks for the help, I've found a solution for the problem. I'm going to post an uncommented version of the code on pastebin, becuase I couldn't get the code saple thing working here.

code

Bob123
  • 1
  • 3