0

I have a json response like this:

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

And I have a string with this text:

"Watch Take Off 2017 Malayalam Full Movie Online Free"

Now I need to get the linkextid of

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

from JSON response. There is one more thing that I have so much similar data on the JSON response, but I need to the get name that matches maximum words from string using PHP.

Pratik Gadoya
  • 1,420
  • 1
  • 16
  • 27
sumonst21
  • 67
  • 1
  • 11

2 Answers2

0

Not the best solution in the world but this should work:

<?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);
?>

The code just parses your JSON into a variable and than goes through all the 'files' comparing their names with the string you provided the determine the best match. After that it just return the linkextid.

I Didn't tested this code but the important is to get the idea since you will have to adapt it to be more generic anyway.

Daniel
  • 597
  • 6
  • 15
  • Thanks Daniel, I'll give it a try and let u know within some time, also I'll be grateful if u could provide ur recommendation. it is not required to use words, that was my thought to get best match. – sumonst21 Nov 10 '17 at 13:40
  • @sumonst21 there are [some algorithms](https://stackoverflow.com/questions/577463/finding-how-similar-two-strings-are) to determine string similarity, in your case the Damerau-Levenshtein seems to be the best option. You can find some PHP implementations out there, [here](https://github.com/Oefenweb/damerau-levenshtein) for example (didn't test it). Maybe one simple approach you could take to improve the results without adding dependencies and much complexity is to just strip words like "Free", "Online", "Watch", etc of your string before comparing it with the movies in the list. – Daniel Nov 10 '17 at 15:55
  • Daniel, I'm getting this error when using ur function: PHP Fatal error: Cannot use object of type stdClass as array in /home/omwio/public_html/wp-content/themes/WPTube/functions.php on line 832 – sumonst21 Nov 10 '17 at 17:50
  • Indeed there is an error in the code, I've edited it to correct the misstake. It's just a matter of add a second parameter to `json_decode()` with a `true`, like this: `json_decode($yourJsonData, true);`. – Daniel Nov 10 '17 at 17:57
  • i have resolved the above error by using $data = json_decode($response, true); im getting data now but wrong linkextid – sumonst21 Nov 10 '17 at 18:00
  • i think i u wanted to add some codes myself for other words where u used only two words right? if yes then I'll be grateful if u could help me about it. I'm not that much exert actually to fix this issue myself :( – sumonst21 Nov 10 '17 at 18:02
  • as my understanding ur current code is only matching two words, i very hope i will get the correct data if we could match up to 5 words. – sumonst21 Nov 10 '17 at 18:06
  • The code match all words, not just two. The best way to debug and find out what's happening in order to make it work is to put some `print_r()` statements in the code to see what's in each variable at a each step of the execution. – Daniel Nov 10 '17 at 18:15
  • u meant this? print_r(get_movie_linkextid($data, $name)); I'm looking for the movie for this title: Villain 2017 Malayalam Full Movie Watch Online Free. and the movie name on json is: "name": "Villain (2017).avi.mp4", but using ur code Im getting the linkextid of "name": "Transformers - The Last Knight SCR.mkv.mp4", but as u can see transformers does not even contains the word villain so how this showing as a matching result? :( – sumonst21 Nov 10 '17 at 18:19
  • This serves to see what the function `get_movie_linkextid` is returning. Read the code, try to understand what it's doing and use `print_r` or `echo` to see what's in each variable, it'll help you figure it out. – Daniel Nov 10 '17 at 18:22
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'])

?>

  • Thanks Himanshu Patel, ur answer looks like that it should work, I'll try using all answers and will choose the best one. – sumonst21 Nov 10 '17 at 13:43
  • this not giving me the correct info also getting this error on errorlog: PHP Warning: Invalid argument supplied for foreach() in /home/omwio/public_html/wp-content/themes/WPTube/header.php on line 126 – sumonst21 Nov 10 '17 at 17:52