2017-03-09 8 views
1

" - "の前後にある単語をPHPで区切りたい。私はスペースを運ぶテキストと " - "の後ろにあるテキストを分けることができません。phpを使って " - "の前後に単語を壊す方法は?

<?php 
$value="this | is : my , text - test , done > hello-hi"; 
$keywords = preg_split("/[,|:&>]+/", $value); 
print_r($keywords); 
?> 

回答M得る: アレイ([0] =>この[1] => [2] =>私の[3] =>テキストさ - 試験[4] =>行わ[5] = > hello-hi)

回答:私はこのようになります: Array [0] => this [1] =>は[2] => my [3] => text [4] => test [ 5] => [6] =>こんにちは-HI)

+3

'爆発( ' - '、$ valueが、2) ' - ' とそれの後に'の後に文字列を区切ります。 –

+0

http://php.net/manual/en/function.explode.php – Dimi

+0

私はexplodeを使用した後も必要な結果を得られません。 @Pheagey –

答えて

1

私はあなたがgruopを作成し、1 OR|)は-間の単語を分割するために追加できると思います行って。

([,|:&>]+|\s+-\s+)-が続き、1つ以上のスペースに続く1つ以上のスペースのために,|:&>かによって分割されます。これにより、文字列/テキストがhello-hiのように2つの要素に分割されないようにします。

$value="this | is : my , text - test , done > hello-hi"; 
$keywords = preg_split("/([,|:&>]+|\s+-\s+)/", $value); 
print_r($keywords); 

出力:

Array ([0] => this [1] => is [2] => my [3] => text [4] => test [5] => done [6] => hello-hi) 
+0

ありがとう、それは私のために働いた。 –

関連する問題