在其他逗号分隔字段中获取空格分隔字段(循环?)

发布于 2025-01-07 07:16:53 字数 400 浏览 0 评论 0原文

第一个问题回答得很快,谢谢大家。

这是问题,对于给定的字符串..

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

似狗非友 2025-01-14 07:16:53
$str = str_replace(',', '', $str);
$arr = explode(' ', $str);
$str = str_replace(',', '', $str);
$arr = explode(' ', $str);
琉璃梦幻 2025-01-14 07:16:53

如果你想要做的是使用空格或逗号作为分隔符来分割字符串,你可以使用 preg_split:

$str = "field1 field2 field3, field4 field5, field6";

$v = preg_split("/[\s,]+/", $str);

var_dump($v);

你会得到这个:

array(6) {
  [0]=>
  string(6) "field1"
  [1]=>
  string(6) "field2"
  [2]=>
  string(6) "field3"
  [3]=>
  string(6) "field4"
  [4]=>
  string(6) "field5"
  [5]=>
  string(6) "field6"
}

If what you want to do is split the string using either spaces or commas as delimiters you can use preg_split:

$str = "field1 field2 field3, field4 field5, field6";

$v = preg_split("/[\s,]+/", $str);

var_dump($v);

And you'll get this:

array(6) {
  [0]=>
  string(6) "field1"
  [1]=>
  string(6) "field2"
  [2]=>
  string(6) "field3"
  [3]=>
  string(6) "field4"
  [4]=>
  string(6) "field5"
  [5]=>
  string(6) "field6"
}
衣神在巴黎 2025-01-14 07:16:53

我会首先标准化分隔符字符,然后在公共分隔符上爆炸()。

# replace spaces with commas:
$str = str_replace( ' ', ',', $str );
# replace the 'doubled commas' with single commas:
$str = str_replace( ',,', ',', $str );
# now you have normalized input:
print_r( explode(',', $str ) );

i would normalize the separater charater first then explode() on the common separator..

# replace spaces with commas:
$str = str_replace( ' ', ',', $str );
# replace the 'doubled commas' with single commas:
$str = str_replace( ',,', ',', $str );
# now you have normalized input:
print_r( explode(',', $str ) );
起风了 2025-01-14 07:16:53
<?php
    $str = "field1 field2 field3, field4 field5, field6   field7 , field8";

    // even works on more than one space or a comma surrounded by spaces.
    $v = preg_split("~\s*,\s*|\s+~", $str);

    var_dump($v);
?>

输出

array
  0 => string 'field1' (length=6)
  1 => string 'field2' (length=6)
  2 => string 'field3' (length=6)
  3 => string 'field4' (length=6)
  4 => string 'field5' (length=6)
  5 => string 'field6' (length=6)
<?php
    $str = "field1 field2 field3, field4 field5, field6   field7 , field8";

    // even works on more than one space or a comma surrounded by spaces.
    $v = preg_split("~\s*,\s*|\s+~", $str);

    var_dump($v);
?>

output

array
  0 => string 'field1' (length=6)
  1 => string 'field2' (length=6)
  2 => string 'field3' (length=6)
  3 => string 'field4' (length=6)
  4 => string 'field5' (length=6)
  5 => string 'field6' (length=6)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文