使用php获取匹配字符串中最大单词的json对象

时间:2017-11-10 12:57:39

标签: php json

我有这样的json响应:

let animator = UIViewPropertyAnimator(duration: 0.25, curve: .easeOut) {
   ...
}
animator.scrubsLinearly = false

我有一个包含此文字的字符串:

  

"观看2017年马拉雅拉姆全场免费电影#34;

现在我需要获得{ "status": 200, "msg": "OK", "result": { "folders": [ { "id": "3812454", "name": ".subtitles" }, { "id": "3812455", "name": ".videothumb" } ], "files": [ { "name": "Angamaly Diaries HD.MP4.mp4", "cblock": null, "sha1": "fcc2c99f2db6e3e8a700c3247206a1c2148e14cb", "folderid": "3812453", "upload_at": "1510255141", "status": "active", "size": "713544705", "content_type": "video/mp4", "download_count": "0", "cstatus": "ok", "linkextid": "PjUv5IYA2J8" }, { "name": "Take Off 2017.MP4.mp4", "cblock": null, "sha1": "2fe7fb4d45322a085d41239d6429d1cc8e94e2ce", "folderid": "3812453", "upload_at": "1510255141", "status": "active", "size": "954148848", "content_type": "video/mp4", "download_count": "0", "cstatus": "ok", "linkextid": "BIBcjWqF0_I" }, { "name": "Rangoon 2017 Tamil.MP4.mp4", "cblock": null, "sha1": "c685e7c11636982860ae7f34b671a20fc746feee", "folderid": "3812453", "upload_at": "1510255141", "status": "active", "size": "779899588", "content_type": "video/mp4", "download_count": "0", "cstatus": "ok", "linkextid": "00D7GzP6mls" }, { "name": "The Zookeeper’s Wife 2017.MP4.mp4.mp4", "cblock": null, "sha1": "a143faafbd8a6eaf2276f25cd642ac3019d71ffc", "folderid": "3812453", "upload_at": "1510256266", "status": "active", "size": "550126461", "content_type": "video/mp4", "download_count": "0", "cstatus": "ok", "linkextid": "bwUhqbiJJWQ" } ] } }

linkextid
来自JSON响应的

。还有一件事我在JSON响应上有这么多类似的数据,但我需要使用"name": "Take Off 2017.MP4.mp4" 匹配来自字符串的最大单词的get name

2 个答案:

答案 0 :(得分:0)

这不是世界上最好的解决方案,但这应该有效:

<?php

    function compare_strings($s1, $s2) {
        $s1Words = explode(' ', $s1);
        $s2Words = explode(' ', $s2);

        $wordCount = 0;
        foreach ($s1Words as $word) {
            if ( strpos($s2Words, $word) ) {
                $wordCount++;
            }
        }

        return $wordCount;
    }

    function get_movie_linkextid($data, $name) {

        $bestMatchIndex = 1;
        $bestMatchScore = 0;
        foreach ($data['result']['files'] as $i => $movie) {
            $currentScore = compare_strings($movie['name'], $name);
            if ($currentScore > $bestMatchScore) {
                $bestMatchScore = $currentScore;
                $bestMatchIndex = $i;
            }
        }

        return $data['result']['files'][$bestMatchIndex]['linkextid'];
    }

    $data = json_decode($yourJsonData, true);

    $name = "Watch Take Off 2017 Malayalam Full Movie Online Free";

    echo get_movie_linkextid($data, $name);
?>

代码只是将您的JSON解析为变量,而不是遍历所有&#39;文件&#39;将他们的名字与你提供的字符串进行比较,确定最佳匹配。之后,它只返回linkextid

我没有测试过这段代码,但重要的是得到这个想法,因为无论如何你必须使它更具通用性。

答案 1 :(得分:0)

<?php
$json_data='{"status":200,"msg":"OK","result":{"folders":[{"id":"3812454","name":".subtitles"},{"id":"3812455","name":".videothumb"}],"files":[{"name":"Angamaly Diaries HD.MP4.mp4","cblock":null,"sha1":"fcc2c99f2db6e3e8a700c3247206a1c2148e14cb","folderid":"3812453","upload_at":"1510255141","status":"active","size":"713544705","content_type":"video/mp4","download_count":"0","cstatus":"ok","linkextid":"PjUv5IYA2J8"},{"name":"Take Off 2017.MP4.mp4","cblock":null,"sha1":"2fe7fb4d45322a085d41239d6429d1cc8e94e2ce","folderid":"3812453","upload_at":"1510255141","status":"active","size":"954148848","content_type":"video/mp4","download_count":"0","cstatus":"ok","linkextid":"BIBcjWqF0_I"},{"name":"Rangoon 2017 Tamil.MP4.mp4","cblock":null,"sha1":"c685e7c11636982860ae7f34b671a20fc746feee","folderid":"3812453","upload_at":"1510255141","status":"active","size":"779899588","content_type":"video/mp4","download_count":"0","cstatus":"ok","linkextid":"00D7GzP6mls"},{"name":"The Zookeeper’s Wife 2017.MP4.mp4.mp4","cblock":null,"sha1":"a143faafbd8a6eaf2276f25cd642ac3019d71ffc","folderid":"3812453","upload_at":"1510256266","status":"active","size":"550126461","content_type":"video/mp4","download_count":"0","cstatus":"ok","linkextid":"bwUhqbiJJWQ"}]}}';

$data=json_decode($json_data,true);
$files=$data['result']['files'];

$search="Watch Take Off 2017 Malayalam Full Movie Online Free";
$search_array=explode(' ',$search);
foreach($search_array as $key=>$row){
    $search_array[$key]=trim($row);
}

$match=[];
foreach($files as $key=>$row){
    $match_count=0;
    foreach($search_array as $s){
        if(preg_match('/'.$s.'/',$row['name'])){
            $match_count+=1;
        }
    }
    $match[$key]=$match_count;
}
rsort($match);
print_r($files[$match[0]]['linkextid'])

&GT;