PHP - 根据特定字符拆分字符串
PHP - Split string on specific characters
我正在做一个项目,我从政府网站下载了数千条法律(合法性都符合要求)。我正在尝试拆分文件的名称,以便我可以在我的网站上更好地对它们进行排序。文件是这样设计的
48600003801.html
我是 运行 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>";
}
我将如何拆分这样的字符串类型,因为它们几乎都是不同的长度?
试试这个:
// 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>";
}
$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);
我正在做一个项目,我从政府网站下载了数千条法律(合法性都符合要求)。我正在尝试拆分文件的名称,以便我可以在我的网站上更好地对它们进行排序。文件是这样设计的
48600003801.html
我是 运行 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>";
}
我将如何拆分这样的字符串类型,因为它们几乎都是不同的长度?
试试这个:
// 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>";
}
$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);