0 レビュー
2 回答
php-文字列を等しい部分に分解する
$str = "Hello fri3nd, you're looking good today! What a Great day this is! How fancy and cool!";
$pieces = explode(" ",$str, 3);
print_r($pieces);
これにより、 $pieces[0] = 'Hello',
$ piece [1] ='fri3nd' ... and the rest of the string is all shoved into
$piece[3]`が得られます。どうすれば3または4語ごとに爆発することができますか?
わからない
0
レビュー
答え :
解決策:
たぶん:?
<?php
$str = "Hello fri3nd, you're looking good today! What a Great day this is! How fancy and cool!";
$array = array_map(create_function('$a', 'return implode(" ", $a);'), array_chunk(preg_split('/\s+/', $str), 3));
var_dump($array);
説明:
- 最初に、任意の(結合された)空白で文字列を分割します:
preg_split
- 次に、結果の配列を「分割」します:
array_chunk
- 次に、
array_map
を使用してimplode
を結果に適用します単語のグループ
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。