在PHP中,如何保存由空格和行分隔的单词并将单词放在数组中

Lay*_*tPH 0 php arrays string tokenize

我需要你的帮助.我有一个变量名$ thetextstring,其中包含9个单词,分别用LINE BREAKS和SPACES,我是从html表单中获取的.

$thetextstring = "alpha bravo charlie
delta echo
foxtrot
golf hotel india" ;
Run Code Online (Sandbox Code Playgroud)

我怎样才能将php字符串$ thetextstring标记为删除行和空格并将9个单词放入数组中

$thetextarray[0] = "alpha";
$thetextarray[1] = "bravo";
$thetextarray[2] = "charlie";
$thetextarray[3] = "delta";
$thetextarray[4] = "echo";
$thetextarray[5] = "foxtrot";
$thetextarray[6] = "golf";
$thetextarray[7] = "hotel";
$thetextarray[8] = "india";
Run Code Online (Sandbox Code Playgroud)

我需要PHP代码来处理这个问题.非常感谢你提前!

Raj*_*jan 6

使用简单的explode()函数

$str="new sample string";
$str=preg_replace("/\s+/", " ", $str);
$arr=explode(" ",$str);
print_r($arr);
Run Code Online (Sandbox Code Playgroud)

输出:

Array ( [0] => new [1] => sample [2] => string )
Run Code Online (Sandbox Code Playgroud)

  • 不,您需要先移除额外的空白区域和新线条 (2认同)