Search code examples
phpstringsplitfilenamestext-parsing

Parse filename without delimiting characters into segments


I'm working on a project where I downloaded thousands laws from the governments website (legality all in order). I am trying to split the name of the files so I can sort them better in my website. The file is designed as such

48600003801.html

I am running a foreach loop on the scandir() function. I have around 20,000+ files as such. I want to do the following:

Chapter: 486
Section: 380

      CH.   ART.  SEC.
Split 486 | 000 | 0380 | 1

// PHP code
$files = scandir('stathtml/');
    foreach ($files as $file) {

        // Change this to a split function and then echo certain parts
        // of it out to test.
        echo $file . "<br>";

    }

How would I go about splitting such a string type up seeing that they are almost all different lengths?


Solution

  • try this:

    // PHP code
        $files = scandir('stathtml/');
            foreach ($files as $file) {
                $arr1 = substr($files, -5);
                $arr1 = substr($arr1, 4);
                $arr2 = substr($files, 3);
                echo $file . "<br>";
                echo "section ".$arr1 . "<br>";
                echo "chapter".$arr2 . "<br>";
            }