0

あなたの助けが必要です。HTMLフォームから取得したLINE BREAKSとSPACESで区切られた9つの単語を含む変数名$thetextstringがあります。

$thetextstring = "alpha bravo charlie
delta echo
foxtrot
golf hotel india" ;

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";

これを処理するにはphpコードが必要です。事前にどうもありがとうございました!

4

5 に答える 5

6

単純なexpand()関数を使用する

$str="new sample string";
$str=preg_replace("/\s+/", " ", $str);
$arr=explode(" ",$str);
print_r($arr);

出力:

Array ( [0] => new [1] => sample [2] => string )
于 2013-09-04T06:16:07.440 に答える
3

Here is what you want, I removed all additional new line and space.

$thetextstring = "alpha bravo charlie
delta echo
foxtrot
golf hotel india" ;
$thetextstring = preg_replace("#[\s]+#", " ", $thetextstring);
$words = explode(" ", $thetextstring);
print_r($words);

(
    [0] => alpha
    [1] => bravo
    [2] => charlie
    [3] => delta
    [4] => echo
    [5] => foxtrot
    [6] => golf
    [7] => hotel
    [8] => india
)
于 2013-09-04T06:22:04.680 に答える
0

複数の区切り文字で爆発を使用する方法multiexplodeについては、PHP ドキュメントのコメントにあるfunction を参照してください。explode()

http://php.net/manual/en/function.explode.php#111307

于 2013-09-04T06:16:43.613 に答える
0
$thetextstring = "alpha bravo charlie
delta echo
foxtrot
golf hotel india" ;

$string = trim(preg_replace('/\s+/', ' ', $thetextstring));

$result =  explode(" ", $thetextstring);

print_r( $result );

最初に、指定された文字列からすべての改行を削除して、改行文字/改行のない文字列が 1 行だけであることを明確にする必要があります。

次に、Explode 関数は、スペースで区切られた指定された文字列から配列を作成します。

last 結果を出力して、各単語を配列内の単一のエンティティとして表示できます。

于 2013-09-04T06:24:17.540 に答える
0
$thetextstring = "alpha bravo charlie delta echo foxtrot golf hotel india" ; 

$c=  explode(" ", $thetextstring);
print_r($c);
于 2013-09-04T06:16:53.290 に答える