最初に文字列からすべてのスペースを削除せずpreg_split
に、結果の末尾のスペースを削除するにはどうすればよいですか?preg_replace
$test
$test = 'One , Two, Thee ';
$test = preg_replace('/\s+/', ' ', $test);
$pieces = preg_split("/[,]/", $test);
最初に文字列からすべてのスペースを削除せずpreg_split
に、結果の末尾のスペースを削除するにはどうすればよいですか?preg_replace
$test
$test = 'One , Two, Thee ';
$test = preg_replace('/\s+/', ' ', $test);
$pieces = preg_split("/[,]/", $test);
そうでなければならない場合preg_split()
(実際に質問でそれが必要だった場合)、これが役立つ場合があります:
$test = 'One , Two, Thee ';
$pieces = preg_split("/\s*,\s*/", trim($test), -1, PREG_SPLIT_NO_EMPTY);
trim()
最初の要素の前と最後の要素の後ろのスペースを削除するために使用されます。(これpreg_split()
はしません - コンマの周りのスペースだけを削除します)
私は次のようにします:
$test = 'One , Two, Thee ';
$pieces = array_map('trim', explode(',', $test));
print_r($pieces);