2016-10-17 7 views
1

正規表現コードpreg_split(:)次のコードの場合

$string = "hello: Mister, Winterbottom"; 

$words = preg_split("/[\s,]+/", $string); 
print_r ($words); 

私が手:

Array ([0] => hello: [1] => Mister [2] => Winterbottom) 

が、私は結果になりたい:

Array ([0] => hello [1] => Mister [2] => Winterbottom) 

それがするように、コロンを無視する。どうしたらいいですか?

+2

使用 'を/を参照してください+ /'。または答えのために '/ \ W + /' –

答えて

1

あなたは、:を使用して文字クラスを拡張するだけで、その中にそれを入れて、

/[\s,:]+/ 

を使用する必要がある場合its demo hereを参照してください。または、/\W+/を使用して、1文字以上の単語以外の文字で分割します。

$words = preg_split("/[\s,:]+/", $string); 
print_r ($words); 
// Or 
print_r(preg_split("/\W+/", $string)); 

[、\ S:] PHP demo

+1

thx。最初のものは私にとって完璧に機能します! –

0
$string = "hello: Mister, Winterbottom"; 

$words = preg_split("/[\s,]+/", $string); 

$words[0] = rtrim($words[0],":"); 

print_r ($words); 
+0

回答には説明が必要です。 – chris85

+0

このコードスニペットは問題を解決するかもしれませんが、[説明を含む](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)は本当にあなたの投稿の質を向上させるのに役立ちます。将来読者の質問に答えていることを覚えておいてください。そうした人々はあなたのコード提案の理由を知らないかもしれません。 – andreas

関連する問題