Search code examples
phpparsingpreg-match

how to parse data after equal sign ="title 1"?


I have a string as follow:

$value[0]='#EXTINF:-1 tvg-name="title 1"  tvg-logo="./Logo1.png" group-title="groupTitle1",title 1';

I want to get data of tvg-name=, tvg-logo=,group-title= and another value after the last "," which is title 1 in this case. I want to place those data as variables for later use. could any one tell me best way to do it ?


Solution

  • Here's an example without using preg_match but should get you the expected result

    <?php
    function scrape_value($data, $key){
        $start = $key.'="';
        $end = '"';
        $data = stristr($data, $start); // Stripping all data from before $start
        $data = substr($data, strlen($start));  // Stripping $start
        $stop = stripos($data, $end);   // Getting the position of the $end of the data to scrape
        $data = substr($data, 0, $stop);    // Stripping all data from after and including the $end of the data to scrape
        return $data;   // Returning the scraped data from the function
    }
    $value[0] = '#EXTINF:-1 tvg-name="title 1"  tvg-logo="./Logo1.png" group-title="groupTitle1",title 1';
    echo scrape_value($value[0], 'tvg-name'); echo '<br />';
    echo scrape_value($value[0], 'tvg-logo'); echo '<br />';
    echo scrape_value($value[0], 'group-title'); echo '<br />';
    echo substr($value[0],strrpos($value[0],',')+1);
    ?>
    

    Result for tvg-name = title 1

    Result for tvg-logo = ./Logo1.png

    Result for group-title = groupTitle1

    Result for last value = title 1