PHP Find directory telling part of directory name

2

Hello

I already have an application prototype running in PHP, where I access photos from a certain directory. However, the directories were not very user friendly, for example:

2016/783/01/2016-07-26-00001

To get a little more friendly, it would be interesting that the third and fourth elements that are respectively category and event, could have a name to the right.

Looking like this:

2016/783/01-relatorios/2016-07-26-00001-relatorio semanal 21

What I would like was to access this same folder but completely ignoring the path's texts, where basically the code would ignore the texts.

I never needed to do this, I'm reading everything about Filesystem and related, but I still have not found a solution!

These folders are all linked with database, in theory there is no need to do this inclusion of the texts in the way, however, I am thinking of implementing this, because in the future I believe that someone who will tamper with Backup will end me causing problems because he is not understanding anything.

Thank you!

    
asked by anonymous 26.07.2016 / 18:58

1 answer

0

This should serve what you want:

<?php
$dir = "2016/783/01-relatorios/2016-07-26-00001-relatorio semanal 21";

$peaces = explode("/", $dir);

foreach ($peaces as $idx => $peace) {
    if (preg_match("/-\D+/", $peace, $match)) {
        $new_peace = substr($peace, 0, strpos($peace, $match[0]));
        $peaces[$idx] = $new_peace;
    }
}

$dir = implode("/", $peaces);
echo $dir;
?>

We are looking for a pattern by the (-) character followed by no digits.

The output will be:

2016/783/01/2016-07-26-00001
    
27.07.2016 / 21:26