簡體   English   中英

使用php從字符串中獲取匹配最大單詞的json對象

[英]Get json object that matching maximum words from a string using php

我有一個像這樣的json響應:

{
"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"
        }
    ]
}
}

我有一個帶有以下文本的字符串:

“觀看起飛2017年馬拉雅拉姆語完整電影在線免費”

現在,我需要得到linkextid

"name": "Take Off 2017.MP4.mp4" 

來自JSON響應。 還有一件事,我在JSON響應上有很多類似的數據,但是我需要獲取與使用PHP字符串中的最大單詞匹配的name

這不是世界上最好的解決方案,但這應該可以工作:

<?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解析為一個變量,然后遍歷所有“文件”,將它們的名稱與您提供的字符串進行比較,以確定最佳匹配。 之后,它只返回linkextid

我沒有測試此代碼,但重要的是要弄清這個主意,因為無論如何您都必須對其進行修改以使其更加通用。

<?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'])

?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM