最後の番号を除いて、すべての番号を削除したいと思います。
例:
test test 1 1 1 255 255 test 7.log
私は次の方法で変換したい:
test test test 255 7.log
さまざまな組み合わせを試しましたが、この結果で見つけた最良のものは間違っています。
test test 55 test 7.log
このサイトは素晴らしいです。
最後を除くすべての番号を削除する必要がある場合:
$file = "test test 1 1 1 255 255 test 7.log";
list($name, $ext) = explode('.', $file);
// split the file into chunks
$chunks = explode(' ', $name);
$new_chunks = array();
// find all numeric positions
foreach($chunks as $k => $v) {
if(is_numeric($v))
$new_chunks[] = $k;
}
// remove the last position
array_pop($new_chunks);
// for any numeric position delete if from our list
foreach($new_chunks as $k => $v) {
unset($chunks[$v]);
}
// merge the chunks again.
$file = implode(' ', $chunks) . '.' .$ext;
var_dump($file);
出力:
string(20) "test test test 7.log"
重複する番号をすべて削除する場合は、次のようにします。
$file = "test test 1 1 1 255 255 test 7.log";
list($name, $ext) = explode('.', $file);
$chunks = explode(' ', $name);
$new_chunks = array();
$output = array();
foreach($chunks as $k => $v) {
if(is_numeric($v)){
if(!in_array($v, $new_chunks)) {
$output[] = $v;
$new_chunks[] = $v;
}} else
$output[] = $v;
}
var_dump(implode(' ', $output). '.' .$ext);
出力:
string(26) "test test 1 255 test 7.log"