【问题标题】:PHP: Stripping protocol from Instagram API image urlsPHP:从 Instagram API 图像 url 中剥离协议
【发布时间】:2014-11-22 08:29:35
【问题描述】:

这是来自 Instagram JSON 的 sn-p:

"images":{
    "standard_resolution":
        { "url":"http:\/\/scontent-a.cdninstagram.com\/hphotos-xfa1\/t51.2885-15\/10593467_370803786404217_1595289732_n.jpg","width":640,"height":640}
}

我想从['images']['standard_resolution']['url'] 中剥离协议。我试过了:

.parse_url($value['images']['standard_resolution']['url'], PHP_URL_HOST) . parse_url($value['images']['standard_resolution']['url'], PHP_URL_PATH).

但是什么都不做!我认为这与在 JSON (http:\/\/) 中完成的/ 转义有关。因为如果我尝试

.parse_url("http://www.google.com", PHP_URL_HOST) . parse_url("http://www.google.com", PHP_URL_PATH).

.. 它工作正常。我想让事情变得简单......并且不要使用正则表达式。 parse_url 会很完美。

【问题讨论】:

标签: php regex json parse-url


【解决方案1】:

为什么您甚至需要进行替换或正则表达式?

如果您使用json_decode,则转义的斜杠不会被转义。

这样做:

<?php

$foo = '{"images":{"standard_resolution":{"url":"http:\/\/scontent-a.cdninstagram.com\/hphotos-xfa1\/t51.2885-15\/10593467_370803786404217_1595289732_n.jpg","width":640,"height":640}}}';
$bar = json_decode($foo, true);

$baz = 
parse_url($bar['images']['standard_resolution']['url'], PHP_URL_HOST) . 
parse_url($bar['images']['standard_resolution']['url'], PHP_URL_PATH);

echo $baz;

会输出:

scontent-a.cdninstagram.com/hphotos-xfa1/t51.2885-15/10593467_370803786404217_1595289732_n.jpg

见:

http://ideone.com/F0c4m9

【讨论】:

    猜你喜欢
    • 2013-10-17
    • 1970-01-01
    • 2023-03-20
    • 2018-10-29
    • 2017-01-17
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 2015-12-22
    相关资源
    最近更新 更多