PHP - 在特定字符上拆分字符串

时间:2022-12-29 20:21:35

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:

我在scandir()函数上运行foreach循环。我有大约20,000多个文件。我想做以下事情:

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?

我怎么会分裂这样的字符串类型,看到它们几乎都是不同的长度?

2 个解决方案

#1


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>";
        }

#2


$array=[0 => '65437890657.html', 1 => '987121340098.html', 2 => '98073517639.html'];
$i=0; $s=substr; $arrey=[];
foreach($array as $file) {
    $file = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file);
    $arrey[$i]['chapter']=$s($file, 0, 3);
    $arrey[$i]['article']=$s($file, 3, 3);
    $arrey[$i]['section']=$s($file, 6, 4);
    $arrey[$i]['irrelevant']=$s($file, -1, 1);
    $i++;
}

print_r($arrey);

#1


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>";
        }

#2


$array=[0 => '65437890657.html', 1 => '987121340098.html', 2 => '98073517639.html'];
$i=0; $s=substr; $arrey=[];
foreach($array as $file) {
    $file = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file);
    $arrey[$i]['chapter']=$s($file, 0, 3);
    $arrey[$i]['article']=$s($file, 3, 3);
    $arrey[$i]['section']=$s($file, 6, 4);
    $arrey[$i]['irrelevant']=$s($file, -1, 1);
    $i++;
}

print_r($arrey);