选择JSON字符串的特定部分并在PHP中处理

时间:2015-03-10 22:19:24

标签: php json

我正在尝试用PHP处理一些JSON,它现在工作正常,但我真的不知道如何处理字符串的单个部分。这是JSON脚本:

{
  "films": [
    {
      "film_id": "16543",
      "film_name": "Jurassic Park III (2001)",
      "film_icon": "https:\/\/lh4.googleusercontent.com\/-RxTBi31EAX4\/VLUjlrnoolI\/AAAAAAAAKoQ\/udX-Xozgvz4\/1415445506a.jpg",
      "film_link": "https:\/\/redirector.googlevideo.com\/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=4b47af19340093d8&itag=18&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1428161445&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=CE1E06704C481EFC9DB30ECFFE9E7603E4EB1588.C63D78665AFA36AD232F2BE83CA57D9E2047F982&key=lh1#360#https:\/\/redirector.googlevideo.com\/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=4b47af19340093d8&itag=22&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1428161445&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=32060DD87A9287F2023A5077BD090E09E4F56BD6.9DAF8F78DCB261F72F881D9B308361ADD1B0985A&key=lh1#720#https:\/\/redirector.googlevideo.com\/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=4b47af19340093d8&itag=37&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1428161445&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=30AC94E2F22ADD6ECB15C66A99BC2C7A87F6899E.3E8BE6D0C2AB23D34154661AB332760926F97EAE&key=lh1#1080#",
      "film_count": "10",
      "film_sort": "3"
    },
    {
      "film_id": "17071",
      "film_name": " The Lost World: Jurassic Park (1997)",
      "film_icon": "https:\/\/lh5.googleusercontent.com\/-cKdwk1ZLvX4\/VLUjmhopI0I\/AAAAAAAAKno\/pui10oPTE2Y\/1415510075a.jpg",
      "film_link": "https:\/\/redirector.googlevideo.com\/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=266dd114ac72413c&itag=18&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1428161445&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=D41A734EF7D85EF0ED63BA9FD6E6334E5ED03713.9EFB010555B5AAEE09E2A7F8D1AA0EAC3D514478&key=lh1#360#https:\/\/redirector.googlevideo.com\/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=266dd114ac72413c&itag=22&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1428161445&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=CD9512B7D430980F9F0BD80177D99964FEF02483.5D80477BDEE53195448284A75BE343242E57713B&key=lh1#720#https:\/\/redirector.googlevideo.com\/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=266dd114ac72413c&itag=37&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1428161445&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=BF1C57787BD814984587EBEAA643F693F4DC418.B3445880ECE36BEB1C665E4E1C471EF7C7F7034C&key=lh1#1080#",
      "film_count": "10",
      "film_sort": "2"
    },
    {
      "film_id": "17070",
      "film_name": "Jurassic Park (1993)",
      "film_icon": "https:\/\/lh3.googleusercontent.com\/-eRAXqWQsXfU\/VLUjmP9Z8mI\/AAAAAAAAKng\/Ru3yngcxgo4\/1415510026a.jpg",
      "film_link": "https:\/\/redirector.googlevideo.com\/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=128668d7aed696fb&itag=18&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1428161445&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=C20FEA7A1EC3B1BD13B36093DEC722B1A30B43A.90A7165FF74AE213EFE6B32E3B13A7F2F16C4665&key=lh1#360#https:\/\/redirector.googlevideo.com\/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=128668d7aed696fb&itag=22&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1428161445&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=9D037782B5F442484EB81AABD738059C629ADD51.C87BDCAEFE4403CE7A2627BB9F4C930BC465033&key=lh1#720#https:\/\/redirector.googlevideo.com\/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=128668d7aed696fb&itag=37&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1428161445&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=CB7826BF0594375D488213E7484D955987126648.BDD3F29107D1E2E65C469142556CE16FB8EE8642&key=lh1#1080#",
      "film_count": "10",
      "film_sort": "1"
    }
  ],
  "catalog_desc": "Overview\r\nAn American science fiction adventure film series based on the novel of the same name by Michael Crichton. The films center on the fictional Isla Nublar near Costa Rica in the Central American Pacific Coast, where a billionaire philanthropist and a small team of genetic scientists have created an amusement park of cloned dinosaurs",
  "catalog_icon": "a",
  "success": 1,
  "message": "successful"
}

正如您所看到的,film_link部分中有多个链接

"film_link": "https:\/\/redirector.googlevideo.com\/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=4b47af19340093d8&itag=18&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1428161445&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=CE1E06704C481EFC9DB30ECFFE9E7603E4EB1588.C63D78665AFA36AD232F2BE83CA57D9E2047F982&key=lh1#360#
https:\/\/redirector.googlevideo.com\/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=4b47af19340093d8&itag=22&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1428161445&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=32060DD87A9287F2023A5077BD090E09E4F56BD6.9DAF8F78DCB261F72F881D9B308361ADD1B0985A&key=lh1#720#
https:\/\/redirector.googlevideo.com\/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=4b47af19340093d8&itag=37&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1428161445&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=30AC94E2F22ADD6ECB15C66A99BC2C7A87F6899E.3E8BE6D0C2AB23D34154661AB332760926F97EAE&key=lh1#1080#
",

但所有这些都在同一个字符串中。如何让PHP只选择最后一个以#1080#?

结尾的链接

谢谢< 3并抱歉我的英语不好: - /

2 个答案:

答案 0 :(得分:1)

您可以通过为the URL fragment一遍又一遍地解析整个字符串来获取最后一个网址,但不要太频繁:

$links = 'https://redirector.googlevideo.com/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=4b47af19340093d8&itag=18&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1428161445&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=CE1E06704C481EFC9DB30ECFFE9E7603E4EB1588.C63D78665AFA36AD232F2BE83CA57D9E2047F982&key=lh1#360#https://redirector.googlevideo.com/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=4b47af19340093d8&itag=22&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1428161445&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=32060DD87A9287F2023A5077BD090E09E4F56BD6.9DAF8F78DCB261F72F881D9B308361ADD1B0985A&key=lh1#720#https://redirector.googlevideo.com/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=4b47af19340093d8&itag=37&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1428161445&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=30AC94E2F22ADD6ECB15C66A99BC2C7A87F6899E.3E8BE6D0C2AB23D34154661AB332760926F97EAE&key=lh1#1080#';

$link = $links;

$i = max(0, substr_count($link, '#') - 2);
while ($i--)
{
    $link = parse_url($link, PHP_URL_FRAGMENT);
}

完成后,$link包含您要查找的网址:

  

https://redirector.googlevideo.com/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=4b47af19340093d8&itag=37&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1428161445&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=30AC94E2F22ADD6ECB15C66A99BC2C7A87F6899E.3E8BE6D0C2AB23D34154661AB332760926F97EAE&key=lh1#1080#

始终会使用最后一个网址。

答案 1 :(得分:0)

    $s='{
  "films": [
    {
      "film_id": "16543",
      "film_name": "Jurassic Park III (2001)",
      "film_icon": "https:\/\/lh4.googleusercontent.com\/-RxTBi31EAX4\/VLUjlrnoolI\/AAAAAAAAKoQ\/udX-Xozgvz4\/1415445506a.jpg",
      "film_link": "https:\/\/redirector.googlevideo.com\/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=4b47af19340093d8&itag=18&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1428161445&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=CE1E06704C481EFC9DB30ECFFE9E7603E4EB1588.C63D78665AFA36AD232F2BE83CA57D9E2047F982&key=lh1#360#https:\/\/redirector.googlevideo.com\/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=4b47af19340093d8&itag=22&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1428161445&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=32060DD87A9287F2023A5077BD090E09E4F56BD6.9DAF8F78DCB261F72F881D9B308361ADD1B0985A&key=lh1#720#https:\/\/redirector.googlevideo.com\/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=4b47af19340093d8&itag=37&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1428161445&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=30AC94E2F22ADD6ECB15C66A99BC2C7A87F6899E.3E8BE6D0C2AB23D34154661AB332760926F97EAE&key=lh1#1080#",
      "film_count": "10",
      "film_sort": "3"
    },
    {
      "film_id": "17071",
      "film_name": " The Lost World: Jurassic Park (1997)",
      "film_icon": "https:\/\/lh5.googleusercontent.com\/-cKdwk1ZLvX4\/VLUjmhopI0I\/AAAAAAAAKno\/pui10oPTE2Y\/1415510075a.jpg",
      "film_link": "https:\/\/redirector.googlevideo.com\/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=266dd114ac72413c&itag=18&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1428161445&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=D41A734EF7D85EF0ED63BA9FD6E6334E5ED03713.9EFB010555B5AAEE09E2A7F8D1AA0EAC3D514478&key=lh1#360#https:\/\/redirector.googlevideo.com\/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=266dd114ac72413c&itag=22&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1428161445&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=CD9512B7D430980F9F0BD80177D99964FEF02483.5D80477BDEE53195448284A75BE343242E57713B&key=lh1#720#https:\/\/redirector.googlevideo.com\/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=266dd114ac72413c&itag=37&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1428161445&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=BF1C57787BD814984587EBEAA643F693F4DC418.B3445880ECE36BEB1C665E4E1C471EF7C7F7034C&key=lh1#1080#",
      "film_count": "10",
      "film_sort": "2"
    },
    {
      "film_id": "17070",
      "film_name": "Jurassic Park (1993)",
      "film_icon": "https:\/\/lh3.googleusercontent.com\/-eRAXqWQsXfU\/VLUjmP9Z8mI\/AAAAAAAAKng\/Ru3yngcxgo4\/1415510026a.jpg",
      "film_link": "https:\/\/redirector.googlevideo.com\/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=128668d7aed696fb&itag=18&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1428161445&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=C20FEA7A1EC3B1BD13B36093DEC722B1A30B43A.90A7165FF74AE213EFE6B32E3B13A7F2F16C4665&key=lh1#360#https:\/\/redirector.googlevideo.com\/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=128668d7aed696fb&itag=22&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1428161445&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=9D037782B5F442484EB81AABD738059C629ADD51.C87BDCAEFE4403CE7A2627BB9F4C930BC465033&key=lh1#720#https:\/\/redirector.googlevideo.com\/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=128668d7aed696fb&itag=37&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1428161445&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=CB7826BF0594375D488213E7484D955987126648.BDD3F29107D1E2E65C469142556CE16FB8EE8642&key=lh1#1080#",
      "film_count": "10",
      "film_sort": "1"
    }
  ],
  "catalog_desc": "Overview\r\nAn American science fiction adventure film series based on the novel of the same name by Michael Crichton. The films center on the fictional Isla Nublar near Costa Rica in the Central American Pacific Coast, where a billionaire philanthropist and a small team of genetic scientists have created an amusement park of cloned dinosaurs",
  "catalog_icon": "a",
  "success": 1,
  "message": "successful"
}';



$json=json_decode( $s, true );
foreach( $json['films'] as $key => $film ){
    if( isset( $film['film_link'] ) ) {
        preg_match_all( '@#\d+#@',$film['film_link'],$matches );
        print_r( $matches );
    }
}