PHP : 如何将一个字符串分成 4 个部分?
PHP : how separate a string into 4 parts?
我正在尝试将一个字符串分成 4 个部分:
$str = 'Bill Gates - Founder and CEO - Microsoft'
$first_name = 'Bill';
$last_name = 'Gates';
$position = 'Founder and CEO';
$company= 'Microsoft';
如何在 PHP 中实现这些目标?
我觉得我必须使用破折号和空格...但无法弄清楚哪个功能最有效...有什么想法吗?
非常感谢!
使用分解字符串
https://www.php.net/manual/en/function.explode.php
1.
你可以说用 [white-space] 分割字符串,但在这种情况下你会得到
$return_array = explode(" ", "Bill Gates - Founder and CEO - Microsoft");
["Bill", "Gates", "-", "Founder", "and", "CEO", "-", "Microsoft"]
2.
如果你说用 [white-space 破折号 white-space] 分割,你会得到
$return_array = explode(" - ", "Bill Gates - Founder and CEO - Microsoft");
["Bill Gates", "Founder and CEO", "Microsoft"]
如果对于名称部分,您假设总是至少有两个词,您可以创建一个非常简单的正则表达式来一次性获取它:
$str = 'Bill Gates - Founder and CEO - Microsoft';
preg_match('/^(.+) (.+) - (.+) - (.+)$/', $str, $parts);
print_r($parts);
/*
Array
(
[0] => Bill Gates - Founder and CEO - Microsoft
[1] => Bill
[2] => Gates
[3] => Founder and CEO
[4] => Microsoft
)
*/
或者如果您不想使用数组索引 1 到 4,则使用命名组:
$str = 'Bill Gates - Founder and CEO - Microsoft';
preg_match('/^(?P<firstName>.+) (?P<lastName>.+) - (?P<position>.+) - (?P<company>.+)$/', $str, $parts);
print_r($parts);
/*
Array
(
[firstName] => Bill
[lastName] => Gates
[position] => Founder and CEO
[company] => Microsoft
// but also:
[0] => Bill Gates - Founder and CEO - Microsoft
[1] => Bill
[2] => Gates
[3] => Founder and CEO
[4] => Microsoft
)
*/
要完成 uVolpus 答案:
'explode' 是这种情况下的最佳解决方案,但重要的是对数组进行双重分解以获得名字和姓氏。
完整代码如下:
$str = 'Bill Gates - Founder and CEO - Microsoft';
$array = explode(" - ", $str);
$new_array = array();
$new_array['full_name'] = $full_name = $array[0];
$new_array['first_name'] = $first_name = explode(' ',$array[0])[0];
$new_array['last_name'] = $last_name = explode(' ',$array[0])[1];;
$new_array['position'] = $position = $array[1];
$new_array['company'] = $company = $array[2];
echo "new_array"; print_r($new_array);
我正在尝试将一个字符串分成 4 个部分:
$str = 'Bill Gates - Founder and CEO - Microsoft'
$first_name = 'Bill';
$last_name = 'Gates';
$position = 'Founder and CEO';
$company= 'Microsoft';
如何在 PHP 中实现这些目标? 我觉得我必须使用破折号和空格...但无法弄清楚哪个功能最有效...有什么想法吗?
非常感谢!
使用分解字符串
https://www.php.net/manual/en/function.explode.php
1.
你可以说用 [white-space] 分割字符串,但在这种情况下你会得到
$return_array = explode(" ", "Bill Gates - Founder and CEO - Microsoft");
["Bill", "Gates", "-", "Founder", "and", "CEO", "-", "Microsoft"]
2.
如果你说用 [white-space 破折号 white-space] 分割,你会得到
$return_array = explode(" - ", "Bill Gates - Founder and CEO - Microsoft");
["Bill Gates", "Founder and CEO", "Microsoft"]
如果对于名称部分,您假设总是至少有两个词,您可以创建一个非常简单的正则表达式来一次性获取它:
$str = 'Bill Gates - Founder and CEO - Microsoft';
preg_match('/^(.+) (.+) - (.+) - (.+)$/', $str, $parts);
print_r($parts);
/*
Array
(
[0] => Bill Gates - Founder and CEO - Microsoft
[1] => Bill
[2] => Gates
[3] => Founder and CEO
[4] => Microsoft
)
*/
或者如果您不想使用数组索引 1 到 4,则使用命名组:
$str = 'Bill Gates - Founder and CEO - Microsoft';
preg_match('/^(?P<firstName>.+) (?P<lastName>.+) - (?P<position>.+) - (?P<company>.+)$/', $str, $parts);
print_r($parts);
/*
Array
(
[firstName] => Bill
[lastName] => Gates
[position] => Founder and CEO
[company] => Microsoft
// but also:
[0] => Bill Gates - Founder and CEO - Microsoft
[1] => Bill
[2] => Gates
[3] => Founder and CEO
[4] => Microsoft
)
*/
要完成 uVolpus 答案:
'explode' 是这种情况下的最佳解决方案,但重要的是对数组进行双重分解以获得名字和姓氏。
完整代码如下:
$str = 'Bill Gates - Founder and CEO - Microsoft';
$array = explode(" - ", $str);
$new_array = array();
$new_array['full_name'] = $full_name = $array[0];
$new_array['first_name'] = $first_name = explode(' ',$array[0])[0];
$new_array['last_name'] = $last_name = explode(' ',$array[0])[1];;
$new_array['position'] = $position = $array[1];
$new_array['company'] = $company = $array[2];
echo "new_array"; print_r($new_array);