php 用一个或多个空格或制表符分解字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1792950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:51:32  来源:igfitidea点击:

Explode string by one or more spaces or tabs

phpregexexplode

提问by DarthVader

How can I explode a string by one or more spaces or tabs?

如何通过一个或多个空格或制表符分解字符串?

Example:

例子:

A      B      C      D

I want to make this an array.

我想把它变成一个数组。

回答by Ben James

$parts = preg_split('/\s+/', $str);

回答by AliAvci

To separate by tabs:

要按制表符分隔:

$comp = preg_split("/[\t]/", $var);

To separate by spaces/tabs/newlines:

用空格/制表符/换行符分隔:

$comp = preg_split('/\s+/', $var);

To seperate by spaces alone:

单独用空格分隔:

$comp = preg_split('/ +/', $var);

$comp = preg_split('/ +/', $var);

回答by schneck

This works:

这有效:

$string = 'A   B C          D';
$arr = preg_split('/[\s]+/', $string);

回答by lucsan

The author asked for explode, to you can use explode like this

作者要求explode,你可以这样用explode

$resultArray = explode("\t", $inputString);

$resultArray = explode("\t", $inputString);

Note: you must used double quote, not single.

注意:您必须使用双引号,而不是单引号。

回答by jheddings

I think you want preg_split:

我想你想要preg_split

$input = "A  B C   D";
$words = preg_split('/\s+/', $input);
var_dump($words);

回答by Brian Schroth

instead of using explode, try preg_split: http://www.php.net/manual/en/function.preg-split.php

而不是使用爆炸,尝试 preg_split:http://www.php.net/manual/en/function.preg-split.php

回答by MPS

In order to account for full width spacesuch as

为了考虑全宽空间,例如

full width

you can extend Bens answer to this:

您可以扩展 Bens 对此的回答:

$searchValues = preg_split("@[\s+ ]@u", $searchString);

Sources:

资料来源:

(I don't have enough reputation to post a comment, so I'm wrote this as an answer.)

(我没有足够的声誉发表评论,所以我写这个作为答案。)

回答by Peter Schaeffer

The answers provided by other folks (Ben James) are quite good and I have used them. As user889030 points out, the last array element may be empty. Actually, the first and last array elements can be empty. The code below addresses both issues.

其他人(本詹姆斯)提供的答案非常好,我已经使用过它们。正如 user889030 指出的那样,最后一个数组元素可能为空。实际上,第一个和最后一个数组元素可以为空。下面的代码解决了这两个问题。

# Split an input string into an array of substrings using any set
# whitespace characters
function explode_whitespace($str) {  
  # Split the input string into an array
  $parts = preg_split('/\s+/', $str);
  # Get the size of the array of substrings
  $sizeParts = sizeof($parts);
  # Check if the last element of the array is a zero-length string
  if ($sizeParts > 0) {
    $lastPart = $parts[$sizeParts-1];
    if ($lastPart == '') {
      array_pop($parts);
      $sizeParts--;
    }
    # Check if the first element of the array is a zero-length string
    if ($sizeParts > 0) {
      $firstPart = $parts[0];
      if ($firstPart == '') 
        array_shift($parts); 
    }
  }
  return $parts;   
}

回答by Trimbak Gopalghare

Explode string by one or more spaces or tabs in php example as follow: 

   <?php 
       $str = "test1 test2   test3        test4"; 
       $result = preg_split('/[\s]+/', $str);
       var_dump($result);  
    ?>

   /** To seperate by spaces alone: **/
    <?php
      $string = "p q r s t";   
      $res = preg_split('/ +/', $string);
      var_dump($res);
    ?>


回答by ghostdog74

@OP it doesn't matter, you can just split on a space with explode. Until you want to use those values, iterate over the exploded values and discard blanks.

@OP没关系,你可以用explode在一个空间上分裂。在您想要使用这些值之前,请迭代分解的值并丢弃空白。

$str = "A      B      C      D";
$s = explode(" ",$str);
foreach ($s as $a=>$b){    
    if ( trim($b) ) {
     print "using $b\n";
    }
}