Php 按一个或多个空格或制表符分解字符串
如何通过一个或多个空格或制表符分解字符串 例如:Php 按一个或多个空格或制表符分解字符串,php,regex,explode,Php,Regex,Explode,如何通过一个或多个空格或制表符分解字符串 例如: A B C D 我想将其设置为一个数组。不要使用explode,请尝试preg_split:此功能: $parts = preg_split('/\s+/', $str); $string = 'A B C D'; $arr = preg_split('/[\s]+/', $string); 我想你想要: @没关系,你可以在一个空间上用explode分割。在您想要使用这些值之前,请迭代分解
A B C D
我想将其设置为一个数组。不要使用explode,请尝试preg_split:此功能:
$parts = preg_split('/\s+/', $str);
$string = 'A B C D';
$arr = preg_split('/[\s]+/', $string);
我想你想要:
@没关系,你可以在一个空间上用explode分割。在您想要使用这些值之前,请迭代分解的值并放弃空白
$str = "A B C D";
$s = explode(" ",$str);
foreach ($s as $a=>$b){
if ( trim($b) ) {
print "using $b\n";
}
}
要按选项卡分隔,请执行以下操作:
$comp = preg_split("/[\t]/", $var);
要按空格/制表符/换行符分隔,请执行以下操作:
$comp = preg_split('/\s+/', $var);
要单独按空格分隔,请执行以下操作:
$comp=preg_split('/+/',$var)编码>以说明全宽空间,例如
full width
您可以将Bens的答案扩展为:
$searchValues = preg_split("@[\s+ ]@u", $searchString);
资料来源:
(我没有足够的声誉发表评论,所以我写这篇文章作为回答。)作者要求使用explode,你可以像这样使用explode
$resultArray=explode(“\t”,$inputString)代码>
注意:您必须使用双引号,而不是单引号。在php示例中按一个或多个空格或制表符分解字符串,如下所示:
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);
?>
/**要单独按空格分隔,请执行以下操作:**/
其他人(本·詹姆斯)提供的答案很好,我也用过。正如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;
}
零个或多个空格意味着每个元素最多只有一个字符,或者有无限多个空元素。你确定这就是你想要的吗?是的,那应该是“一个或多个空格”。制表符分隔的值呢?制表符分隔的值不会分解,所以,$parts last元素将为空。。因此,要移除它,请使用数组_pop($parts);@lucsan的答案必须是最佳答案(),而不是删除最后一个可能为空的部分,可以使用:$parts=preg_split('/\s+/',$str,-1,preg_split_NO_empty)代码>对我有用,比使用正则表达式的黑暗力量要简单一点。但他要求“空格或制表符”,而这只适用于制表符。我来这里也是为了寻找爆炸的空格。我内心深处对此感到悲伤。
# 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;
}