我想删除除最后一个以外的所有数字。
例子:
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"