Zenel Shabani
Zenel Shabani

Reputation: 301

PHP difficult preg_match

I'm trying to manage some files on my personal web server that I use for XBMC but all the files (Movies from YIFY) have names like

Jumanji.1995-720p.YIFY.mp4
Silver.Linings.Playbook.2012.1080p.x264.YIFY.mp4
American Hustle (2013) 1080p BrRip x264 - YIFY.mp4

Notice that some of the items are separated with . and others with _ or spaces.

So what I need to do is to preg_match the file to an array of (title,year,quality) I only know some preg_match basics. But this is way to hard for me.

e.g

echo extract('American Hustle (2013) 1080p BrRip x264 - YIFY.mp4');

This should output =

array(
    'title' => 'American Hustle',
    'year' => '2013',
    'quality' => 1080p
);

Thanks in advance

Upvotes: 4

Views: 153

Answers (2)

vks
vks

Reputation: 67968

^(.*?)\W+(\d{4})(?=[\W ]+?(\d{3,4})p)

You can try this.See demo.

https://regex101.com/r/nS2lT4/29

The regex starts and captures anything from start to a non word letter which can be one or more and has 4 digits ahead of it.After this a lookahead makes sure that after capturing \d{4} there are non word letters which can be one or more and has 4 digits of p ahead of it.Because of lookahead we capture the last 4 digits which have only non word characters and 4 digits p after them.

Upvotes: 5

mmm
mmm

Reputation: 1149

you have 3 differents formats then you need 3 differents parsing types

try this :

$tests = array(
    // format 1
    "Jumanji.1995-720p.YIFY.mp4",
    "Silver.Linings.Playbook.2012-1080p.YIFY.mp4",
    "American.Hustle.2013-1080p.YIFY.mp4",
    // format 2
    "Jumanji.1995.720p.x264.YIFY.mp4",
    "Silver.Linings.Playbook.2012.1080p.x264.YIFY.mp4",
    "American.Hustle.2013.1080p.x264.YIFY.mp4",
    // format 3
    "Jumanji (1995) 720p BrRip x264 - YIFY.mp4",
    "Silver Linings Playbook (2012) 1080p BrRip x264 - YIFY.mp4",
    "American Hustle (2013) 1080p BrRip x264 - YIFY.mp4",
);


function extractInfos($s) {

    $infos = array();

    if (FALSE === strpos($s, ".YIFY.")) {
        // format 3

        $tab = explode(" ", $s);

        $yearIndex = count($tab) - 6;

        $infos["year"] = trim($tab[$yearIndex], "()");
        $infos["quality"] = $tab[$yearIndex + 1];

        array_splice($tab, $yearIndex);
        $infos["title"] = implode(" ", $tab);
    } else {
        // format 1 or 2

        $tab = explode(".", $s);

        $yearIndex = count($tab) - 3;

        if (FALSE === strpos($tab[$yearIndex], "-")) {
            // format 2

            $yearIndex -= 2;

            $infos["year"] = $tab[$yearIndex];
            $infos["quality"] = $tab[$yearIndex + 1];
        } else {
            // format 1
            list($infos["year"], $infos["quality"]) = explode("-", $tab[$yearIndex]);
        }

        array_splice($tab, $yearIndex);
        $infos["title"] = implode(" ", $tab);
    }


    return $infos;
}


echo "<table border=\"1\">";

foreach ($tests as $s) {
    $infos = extractInfos($s);

    ?>
        <tr>
            <td>
                <?php echo $infos["title"];?>
            </td>
            <td>
                <?php echo $infos["year"];?>
            </td>
            <td>
                <?php echo $infos["quality"];?>
            </td>
        </tr>
    <?php
}

echo "</table>";

Upvotes: 1

Related Questions