1

This is a snippet from an Instagram JSON:

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

I'd like to strip the protocol from ['images']['standard_resolution']['url']. I tried with:

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

But does nothing! I think it has to do with the / escaping done in JSON (http:\/\/). Because if I try

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

.. it just works fine. I'd like to keep things easy.. and don't use regex. parse_url would be perfect.

4

1 回答 1

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

于 2014-09-27T14:37:08.753 回答