我想删除后面的所有数字,最后一个除外。
示例:
test test 1 1 1 255 255 test 7.log我想通过以下方式实现转型:
test test test 255 7.log我尝试了许多组合,但我发现这个结果是错误的:
test test 55 test 7.log我感谢每个人的宝贵帮助,这个网站很棒。
发布于 2013-02-22 21:40:51
如果您需要删除除最后一个数字以外的所有数字:
$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"https://stackoverflow.com/questions/15024958
复制相似问题