Php 在特定字符串之后解析字符串

Php 在特定字符串之后解析字符串,php,parsing,Php,Parsing,我有一个字符串($source),其中包含以下数据: {"Title":"War Horse","Year":"2011","Rated":"PG-13","Released":"25 Dec 2011","Runtime":"2 h 26 min","Genre":"Drama, War","Director":"Steven Spielberg","Writer":"Lee Hall, Richard Curtis","Actors":"Jeremy Irvine, Emily Watson

我有一个字符串($source),其中包含以下数据:

{"Title":"War Horse","Year":"2011","Rated":"PG-13","Released":"25 Dec 2011","Runtime":"2 h 26 min","Genre":"Drama, War","Director":"Steven Spielberg","Writer":"Lee Hall, Richard Curtis","Actors":"Jeremy Irvine, Emily Watson, David Thewlis, Benedict Cumberbatch","Plot":"Young Albert enlists to serve in World War I after his beloved horse is sold to the cavalry. Albert's hopeful journey takes him out of England and across Europe as the war rages on.","Poster":"http://ia.media-imdb.com/images/M/MV5BMTU5MjgyNDY2NV5BMl5BanBnXkFtZTcwNjExNDc1Nw@@._V1_SX640.jpg","imdbRating":"7.2","imdbVotes":"39,540","imdbID":"tt1568911","Response":"True"}
我使用以下方法提取标题、流派、情节等:

foreach(str_getcsv($source) as $item) {
    list($k, $v) = explode(':', $item);
    $$k = str_replace('"', '', $v);
    }
到目前为止,这非常有效,我可以使用$Title、$Genre等等。唯一不起作用的是海报的URL,因为我正在分解“:”并且URL-当然-包含“:”(在“http”之后)


如何将海报URL放入变量中?

看起来像JSON数据,为什么不简单:

$txt = '{"Title etc.....}';
$data = json_decode($txt);

$title = $data['Title'];
$genre = $data['Genre'];
etc...
变量非常难看,您可能会因为用JSON数据的内容覆盖其他变量而影响代码


如果你真的坚持要用自动激活的变量来争论你的名称空间,你可以用它来分离看起来像JSON数据的数组,为什么不简单地:

$txt = '{"Title etc.....}';
$data = json_decode($txt);

$title = $data['Title'];
$genre = $data['Genre'];
etc...
变量非常难看,您可能会因为用JSON数据的内容覆盖其他变量而影响代码


如果你真的坚持要用自动激活的变量来争论你的名称空间,你可以用它来分离数组

usejson\u decode

$str = '{"Title":"War Horse","Year":"2011","Rated":"PG-13","Released":"25 Dec 2011","Runtime":"2 h 26 min","Genre":"Drama, War","Director":"Steven Spielberg","Writer":"Lee Hall, Richard Curtis","Actors":"Jeremy Irvine, Emily Watson, David Thewlis, Benedict Cumberbatch","Plot":"Young Albert enlists to serve in World War I after his beloved horse is sold to the cavalry. Albert\'s hopeful journey takes him out of England and across Europe as the war rages on.","Poster":"http://ia.media-imdb.com/images/M/MV5BMTU5MjgyNDY2NV5BMl5BanBnXkFtZTcwNjExNDc1Nw@@._V1_SX640.jpg","imdbRating":"7.2","imdbVotes":"39,540","imdbID":"tt1568911","Response":"True"}';

$decode_string = json_decode($str);

print_r($decode_string);
echo $decode_string->Title;

下面是正在运行的代码

Usejson\u decode

$str = '{"Title":"War Horse","Year":"2011","Rated":"PG-13","Released":"25 Dec 2011","Runtime":"2 h 26 min","Genre":"Drama, War","Director":"Steven Spielberg","Writer":"Lee Hall, Richard Curtis","Actors":"Jeremy Irvine, Emily Watson, David Thewlis, Benedict Cumberbatch","Plot":"Young Albert enlists to serve in World War I after his beloved horse is sold to the cavalry. Albert\'s hopeful journey takes him out of England and across Europe as the war rages on.","Poster":"http://ia.media-imdb.com/images/M/MV5BMTU5MjgyNDY2NV5BMl5BanBnXkFtZTcwNjExNDc1Nw@@._V1_SX640.jpg","imdbRating":"7.2","imdbVotes":"39,540","imdbID":"tt1568911","Response":"True"}';

$decode_string = json_decode($str);

print_r($decode_string);
echo $decode_string->Title;
下面是正在运行的代码

它是一个json

你应该使用

注意,我已经正确地转义了字符串

这是一个json

你应该使用


注意,我已经正确地转义了字符串

这看起来像JSON符号。您应该能够使用
json\u decode()
并像数组一样访问它。您应该能够使用
json\u decode()
并像数组一样访问它。