Php 如何拆分字符串之间具有未知空间量的文本?
所讨论的字符串具有以下性质:Php 如何拆分字符串之间具有未知空间量的文本?,php,Php,所讨论的字符串具有以下性质: "France 1.27" 没有办法知道它们之间的空间大小,因为它会发生变化,但总有一个空间。如何访问每个字符串?您可以使用正则表达式 差不多 $keywords = preg_split("/\s+/", "hello world hi"); print_r($keywords) 可以使用正则表达式 差不多 $keywords = preg_split("/\s+/", "hello wo
"France 1.27"
没有办法知道它们之间的空间大小,因为它会发生变化,但总有一个空间。如何访问每个字符串?您可以使用正则表达式 差不多
$keywords = preg_split("/\s+/", "hello world hi");
print_r($keywords)
可以使用正则表达式 差不多
$keywords = preg_split("/\s+/", "hello world hi");
print_r($keywords)
我可以想出另外两种方法,第一种是删除多个空格:
$string = "France 1.27";
$string = preg_replace('/\s+/', ' ', $string); // Remove double spaces
$string = explode(" ", $string);
或按原样分解并删除空值:
$string = "France 1.27";
$string = explode(" ", $string);
$string = array_filter($string); // Remove empty elements
$string = array_values($string); // Re-index the array, array_filter will mess up the indexes
我可以想出另外两种方法,第一种是删除多个空格:
$string = "France 1.27";
$string = preg_replace('/\s+/', ' ', $string); // Remove double spaces
$string = explode(" ", $string);
或按原样分解并删除空值:
$string = "France 1.27";
$string = explode(" ", $string);
$string = array_filter($string); // Remove empty elements
$string = array_values($string); // Re-index the array, array_filter will mess up the indexes