Search code examples
phpregexstrpos

PHP Fetching URL bellow expression from file


i'm trying to fetch a URL from a file but can't really find a way to make it work.

I've looked into regex and explode and strpos but can't find a way, I could use some help.

This is the piece of code i'm currently using for extracting the channelname;

foreach($fileContent as $line) {
    if((strpos($line, 'group-title') == true) && (strpos($line, 'tvg-name="#####') == false) 
        && ((strpos($line, '|NL|') == true) || (strpos($line, 'group-title="BE"') == true))) {
        $m3uChannelName = substr($line, strrpos($line, ',') + 1);
        $m3uChannelName = rtrim($m3uChannelName);
        $m3uChannelName = mysqli_real_escape_string($conn, $m3uChannelName);
        $sql_insertFileValue = "INSERT INTO epg_m3ufile (m3uChannelName) VALUES ('" . $m3uChannelName . "')";
        $resultM3U = mysqli_query($conn, $sql_insertFileValue);
        
        if (!empty($resultM3U)) {
            $affectedRowM3u ++;
        }else{
            $error_message = mysqli_error($conn) . "\n";
        }
        
    }
}

this is some of the content in de file;

#EXTM3U
#EXTINF:-1 tvg-id="" tvg-name="|NL| NPO 1 HD" tvg-logo="http://134.255.234.197/logos/npo1.png" group-title="NL| NEDERLAND",|NL| NPO 1 HD
http://localhost.dummy.proof:80/123456/654321/0123
#EXTINF:-1 tvg-id="" tvg-name="|NL| NPO 2 HD" tvg-logo="http://134.255.234.197/logos/npo2.png" group-title="NL| NEDERLAND",|NL| NPO 2 HD
http://localhost.dummy.proof:80/654321/123456/3210
#EXTINF:-1 tvg-id="" tvg-name="|NL| NPO 3 HD" tvg-logo="http://134.255.234.197/logos/npo3.png" group-title="NL| NEDERLAND",|NL| NPO 3 HD
http://localhost.dummy.proof:80/416352/524163/0231

What I would like to do is to modify the foreach so that I can import the url aswell as the channelname to my database, but I can't seem to make it work cause the url is on the line bellow the strposs check.

I'm hoping you guys can help me / tell me how to achive this.

I've tried this solution, but couldn't get it to work PHP Preg match all m3u8 to parse into array

Kind regards, Patrick


Solution

  • You might use a pattern with 2 capture groups to get the value of tvg-name and the last url:

    ^#EXTINF:.*?\btvg-name="([^"]*)".*\R(https?:\/\/\S*)
    

    The pattern matches:

    • ^ Start of string
    • #EXTINF: Match literally
    • .*? Match as least as possible characters
    • \btvg-name= A word boundary, match tvg-name=
    • "([^"]*)" Capture in group 1 what is between double quotes
    • .*\R Match the rest of the line and a newline
    • (https?:\/\/\S*) Capture the last url in group 2

    Regex demo

    Example:

    $re = '/^#EXTINF:.*?\btvg-name="([^"]*)".*\R(https?:\/\/\S*)/m';
    $str = '#EXTM3U
    #EXTINF:-1 tvg-id="" tvg-name="|NL| NPO 1 HD" tvg-logo="http://134.255.234.197/logos/npo1.png" group-title="NL| NEDERLAND",|NL| NPO 1 HD
    http://localhost.dummy.proof:80/123456/654321/0123
    #EXTINF:-1 tvg-id="" tvg-name="|NL| NPO 2 HD" tvg-logo="http://134.255.234.197/logos/npo2.png" group-title="NL| NEDERLAND",|NL| NPO 2 HD
    http://localhost.dummy.proof:80/654321/123456/3210
    #EXTINF:-1 tvg-id="" tvg-name="|NL| NPO 3 HD" tvg-logo="http://134.255.234.197/logos/npo3.png" group-title="NL| NEDERLAND",|NL| NPO 3 HD
    http://localhost.dummy.proof:80/416352/524163/0231';
    
    preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
    foreach($matches as $m) {
        echo $m[1] . PHP_EOL . $m[2] . PHP_EOL;
    }
    

    Output

    |NL| NPO 1 HD
    http://localhost.dummy.proof:80/123456/654321/0123
    |NL| NPO 2 HD
    http://localhost.dummy.proof:80/654321/123456/3210
    |NL| NPO 3 HD
    http://localhost.dummy.proof:80/416352/524163/0231