我如何才能获得此网址的ID部分。
281805943&buying=true&liking=false&download=true&sharing=false&show_artwork=false&hide_related=true&show_comments=false&show_playcount=false&show_user=false&color=e55b5b&color_theme=f5f5f5&auto_play=
我目前已经展开了包含API路径+实际链接的网址,但是尝试获取ID 281805943
已经证明有点困难,请注意我们不能只回显{{1因为我正在抓取一个网站并收集他们的网址,所以每次格式保持不变时,这个ID都会有所不同。
原始网址格式如下所示。
281805943
我目前使用此
爆炸了https://w.soundcloud.com/player/?url=https://api.soundcloud.com/tracks/281805943&buying=true&liking=false&download=true&sharing=false&show_artwork=false&hide_related=true&show_comments=false&show_playcount=false&show_user=false&color=e55b5b&color_theme=f5f5f5&auto_play=
答案 0 :(得分:2)
听起来像是来自PHP文档的parse_url的工作......
<?php
$url = 'http://username:password@hostname/path?arg=value#anchor';
print_r(parse_url($url));
$blob = parse_url($url, PHP_URL_PATH);
echo $blob['query']; //arg=value
echo $blob['anchor']; //anchor
答案 1 :(得分:2)
不完全漂亮,但确实找到了ID
$url="https://w.soundcloud.com/player/?url=https://api.soundcloud.com/tracks/281805943&buying=true&liking=false&download=true&sharing=false&show_artwork=false&hide_related=true&show_comments=false&show_playcount=false&show_user=false&color=e55b5b&color_theme=f5f5f5&auto_play=";
preg_match('@/tracks/(\d+)&@',$url,$matches);
echo 'id='.$matches[1];
答案 2 :(得分:2)
假设您的ID始终在&#39;曲目之间/&#39;和&#39;&amp; amp&#39;:
$url = 'https://w.soundcloud.com/player/?url=https://api.soundcloud.com/tracks/281805943&buying=true&liking=false&download=true&sharing=false&show_artwork=false&hide_related=true&show_comments=false&show_playcount=false&show_user=false&color=e55b5b&color_theme=f5f5f5&auto_play=';
$pattern = "/(tracks\/)([\d]+)(\&)/";
preg_match($pattern,$url,$matches,PREG_OFFSET_CAPTURE);
var_dump($matches[2][0]);
答案 3 :(得分:1)
如果格式始终完全相同,那么您可以使用已有的$test[8]
变量执行以下操作:
parse_str($test[8], $output);
$id = key($output);
或单个班轮:
$id = explode('&', $test[8])[0];
皮肤猫的方法有很多种