5 回答
TA贡献2037条经验 获得超6个赞
您可以使用 删除名称中的所有数字,如果新名称的长度小于或大于旧名称,请替换旧名称: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";
}
输出:
joe
joe123
1joe1234
joe
TA贡献1847条经验 获得超7个赞
另一种选择:
<?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"
}
TA贡献2080条经验 获得超4个赞
TA贡献1993条经验 获得超5个赞
我想就是这样:
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);
}
- 5 回答
- 0 关注
- 114 浏览
添加回答
举报