PHP 拆分名称和 phone 号码(文本和号码)
PHP split name and phone number (text and number)
我正在使用 preg_split
将文本和 phone 数字与字符串分开。
我的测试用例如下:
$string_one = "1. Maria +60136777000";
$string_two = "2. Rahul Khan 0146067705";
$string_three = "Johny +6013699900";
$string_four = "Henry 01363456900";
这是我的职能:
function split_them($str) {
return preg_split("/(\D)(\d)/", $str);
}
当我这样使用函数时,phone数字总是不完整:
// string_one
echo "<pre>";
print_r(split_them($string_one));
echo "</pre>";
// output
array(2
0 => 1. Maria
1 => 0136777000 // <--- number is incomplete
)
// string_two
echo "<pre>";
print_r(split_them($string_two));
echo "</pre>";
// output
array(2
0 => 2. Rahul Khan
1 => 146067705 // <--- number is incomplete
)
// string_three
echo "<pre>";
print_r(split_them($string_three));
echo "</pre>";
// output
array(2
0 => Johny
1 => 013699900 // <--- number is incomplete
)
// string_four
echo "<pre>";
print_r(split_them($string_four));
echo "</pre>";
// output
array(2
0 => Henry
1 => 1363456900 // <--- number is incomplete
)
也许我的正则表达式不正确。我错过了什么?
尝试使用 explode() PHP 函数按字符串拆分它们。
例子
$foo = "one two three";
print_r(explode(" ", $foo));
//Output
array (
0 => 'one',
1 => 'two',
2 => 'three'
)
- 试试这个 $iparr = split ("/+/", $ip);打印“$iparr[0]”;
或者像这样:
function split_them($str) {
preg_match("/(.+)\s+(.?\d{5,})/", $str, $matches);
array_shift($matches);
return $matches;
}
输出为:
Array (
[0] => 1. Maria
[1] => +60136777000 )
Array (
[0] => 2. Rahul Khan
[1] => 0146067705 )
Array (
[0] => Johny
[1] => +6013699900 )
Array (
[0] => Hitler
[1] => 01363456900 )
我建议使用以下 preg_split
代码:
preg_split('~\s+(?=\+?\d+$)~', $s)
参见regex demo。
它在最后 1+ 个空格 (\s+
) 处拆分字符串,后跟可选的 +
(\+?
) 和 1+ 个数字 (\d+
) 在字符串的末尾 ($
).
$re = '/\s+(?=\+?\d+$)/';
$strs = ['Johny +6013699900','2. Rahul Khan 0146067705','Johny +6013699900','Henry 01363456900'];
foreach ($strs as $s) {
print_r(preg_split($re, $s));
}
输出:
Array
(
[0] => Johny
[1] => +6013699900
)
Array
(
[0] => 2. Rahul Khan
[1] => 0146067705
)
Array
(
[0] => Johny
[1] => +6013699900
)
Array
(
[0] => Henry
[1] => 01363456900
)
我正在使用 preg_split
将文本和 phone 数字与字符串分开。
我的测试用例如下:
$string_one = "1. Maria +60136777000";
$string_two = "2. Rahul Khan 0146067705";
$string_three = "Johny +6013699900";
$string_four = "Henry 01363456900";
这是我的职能:
function split_them($str) {
return preg_split("/(\D)(\d)/", $str);
}
当我这样使用函数时,phone数字总是不完整:
// string_one
echo "<pre>";
print_r(split_them($string_one));
echo "</pre>";
// output
array(2
0 => 1. Maria
1 => 0136777000 // <--- number is incomplete
)
// string_two
echo "<pre>";
print_r(split_them($string_two));
echo "</pre>";
// output
array(2
0 => 2. Rahul Khan
1 => 146067705 // <--- number is incomplete
)
// string_three
echo "<pre>";
print_r(split_them($string_three));
echo "</pre>";
// output
array(2
0 => Johny
1 => 013699900 // <--- number is incomplete
)
// string_four
echo "<pre>";
print_r(split_them($string_four));
echo "</pre>";
// output
array(2
0 => Henry
1 => 1363456900 // <--- number is incomplete
)
也许我的正则表达式不正确。我错过了什么?
尝试使用 explode() PHP 函数按字符串拆分它们。 例子
$foo = "one two three";
print_r(explode(" ", $foo));
//Output
array (
0 => 'one',
1 => 'two',
2 => 'three'
)
- 试试这个 $iparr = split ("/+/", $ip);打印“$iparr[0]”;
或者像这样:
function split_them($str) {
preg_match("/(.+)\s+(.?\d{5,})/", $str, $matches);
array_shift($matches);
return $matches;
}
输出为:
Array ( [0] => 1. Maria [1] => +60136777000 )
Array ( [0] => 2. Rahul Khan [1] => 0146067705 )
Array ( [0] => Johny [1] => +6013699900 )
Array ( [0] => Hitler [1] => 01363456900 )
我建议使用以下 preg_split
代码:
preg_split('~\s+(?=\+?\d+$)~', $s)
参见regex demo。
它在最后 1+ 个空格 (\s+
) 处拆分字符串,后跟可选的 +
(\+?
) 和 1+ 个数字 (\d+
) 在字符串的末尾 ($
).
$re = '/\s+(?=\+?\d+$)/';
$strs = ['Johny +6013699900','2. Rahul Khan 0146067705','Johny +6013699900','Henry 01363456900'];
foreach ($strs as $s) {
print_r(preg_split($re, $s));
}
输出:
Array
(
[0] => Johny
[1] => +6013699900
)
Array
(
[0] => 2. Rahul Khan
[1] => 0146067705
)
Array
(
[0] => Johny
[1] => +6013699900
)
Array
(
[0] => Henry
[1] => 01363456900
)