简体   繁体   中英

PHP, How can I retrieve a specific number?

I need to retrieve itunes app id from the URL address.

https://itunes.apple.com/us/app/angry-birds-star-wars/id557137623?mt=8

I used strpos(), but this one didn't work.

How can I get numbers after id from this address?

$url = 'https://itunes.apple.com/us/app/angry-birds-star-wars/id557137623?mt=8';

$result = '557137623'

result will be like this one.

$url = 'https://itunes.apple.com/us/app/angry-birds-star-wars/id557137623?mt=8';
preg_match("~id(\d+)~", $url, $matches);
$result = $matches[1];
echo $result; //557137623
// Find where the / is and add 2 for the word "id"
$pos = strrpos($url, "/") + 2; // Note that it's "strrpos" not "strpos"

// Find the question mark at the end
$pos2 = strpos($url, "?");

$id = substr($url, $pos, $pos2 - $pos);

This is, of course, assuming your URL always has this form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM