在其他逗号分隔字段中获取空格分隔字段(循环?)
第一个问题回答得很快,谢谢大家。
这是问题,对于给定的字符串..
$str= "name1 surname2, name2 midname2 surname2";
为了让事情更清楚,有两个人,一个有两个值(名字,姓氏),而另一个也有中间名。所以,我想把它们全部分开,同时知道哪个名字属于哪个人,比如;
foreach ($persons as person){
if( person has midname){
$value1 ="name"; $value2= "midname"; $value3="surname"}
else
$value1="name"; $value2="surname"
}
first question answered very quickly, thanks to all.
here is the issue, for a given string..
$str= "name1 surname2, name2 midname2 surname2";
to make things more clear, there is two persons, one has two values(name, surname) while other has midname too. so, I want to get all them separate while knowing which name belongs to which person, like;
foreach ($persons as person){
if( person has midname){
$value1 ="name"; $value2= "midname"; $value3="surname"}
else
$value1="name"; $value2="surname"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果你想要做的是使用空格或逗号作为分隔符来分割字符串,你可以使用 preg_split:
你会得到这个:
If what you want to do is split the string using either spaces or commas as delimiters you can use preg_split:
And you'll get this:
我会首先标准化分隔符字符,然后在公共分隔符上爆炸()。
i would normalize the separater charater first then explode() on the common separator..
输出
output