-
阿晨1998
您可以使用 删除名称中的所有数字,如果新名称的长度小于或大于旧名称,请替换旧名称:preg_replace$names = array('joe123456', 'joe123', '1joe1234', '123joe456');foreach ($names as $name) { $new_name = preg_replace('/\d/', '', $name); if (strlen($new_name) <= strlen($name) - 6) { $name = $new_name; } echo "$name\n";}输出:joejoe1231joe1234joe3v4l.org 演示
-
aluckdog
另一种选择:<?php$names = array('joe123456', 'joe123', '1joe1234', '123joe456', '1234567joe12345');$new_names = [];foreach ($names as $name) { if (preg_match("/\d{6}+/", $name)) { $new_names[] = preg_replace('/\d/', '', $name); } else { $new_names[] = $name; }}var_dump($new_names);收益 率:array(5) { [0]=> string(3) "joe" [1]=> string(6) "joe123" [2]=> string(8) "1joe1234" [3]=> string(9) "123joe456" [4]=> string(3) "joe"}
-
犯罪嫌疑人X
您可以使用preg_replace来实现此目的。$name = preg_replace('/[0-9]{6,}/', '', $name_with_too_long_numbers);
-
ibeautiful
我想就是这样:preg_match_all('!\d+!', $namewithoutnumber, $matches);$numbers = implode('', $matches[0]);if( strlen($numbers) > 5 ) { $name = str_replace (array ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'), '' , $namewithoutnumber);}
-
皈依舞
当然可以;只需通过 strlen() 函数运行它:if (strlen($namewithoutnumber) > 5) {
$name = str_replace (array ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'), '' , $namewithoutnumber);
}