PHP explode函数接受超过1个空格,PHP,explode,WHITESPACES
我有一个包含两个单词的数组,我试图将它分解为一个空格,但由于某种原因分解时也会计算空格。我该怎么阻止这一切PHP explode函数接受超过1个空格,PHP,explode,WHITESPACES,php,arrays,whitespace,explode,Php,Arrays,Whitespace,Explode,我有一个包含两个单词的数组,我试图将它分解为一个空格,但由于某种原因分解时也会计算空格。我该怎么阻止这一切 <?php $string = "I'm just so peachy, right now"; $string = explode(" ", $string); $count = count($string); $tempCount = 0; while ($tempCount < $count) { echo $string[$tempCount
<?php
$string = "I'm just so peachy, right now";
$string = explode(" ", $string);
$count = count($string);
$tempCount = 0;
while ($tempCount < $count) {
echo $string[$tempCount]."$tempCount<br>";
$tempCount++;
}
?>
预期产出:
使用preg_split,它将使用正则表达式,这样您可以告诉它将所有连续的空格保持为一个
$string = 'I\'m just so peachy, right now';
$spaced = preg_split('~\h+~', $string);
print_r($spaced);
输出:
Array
(
[0] => I'm
[1] => just
[2] => so
[3] => peachy,
[4] => right
[5] => now
)
PHP演示:正则表达式演示:使用preg_分割,它将使用正则表达式,这样您就可以告诉它将所有连续的空格保持为一个
$string = 'I\'m just so peachy, right now';
$spaced = preg_split('~\h+~', $string);
print_r($spaced);
输出:
Array
(
[0] => I'm
[1] => just
[2] => so
[3] => peachy,
[4] => right
[5] => now
)
PHP演示:Regex Demo:h+~是什么意思?水平空白字符(+表示一个或多个字符)。我忘记了
preg\u split()
。很好的解决方案。首先应该添加regex演示。是的,\h
用于内联空格(水平空格)。+
用于前一个字符的一次或多次出现。~
是分隔符。@Zanderwar你们需要标记投票否决你们的人。~\h+~是什么意思?水平空白字符(+表示一个或多个)。我忘记了preg_split()
。很好的解决方案。首先应该添加regex演示。是的,\h
用于内联空格(水平空格)。+
用于前一个字符的一次或多次出现。~
是分隔符。@Zanderwar你们需要标记投票否决你们的人。