2011-06-22 11 views
5

に文字列を分割するにはどうすればよい私は以下のように言葉に文を分割します:
例えば:私は言葉と記号の組み合わせ配列

This is a test from php, python, asp and also from other languages. Alash! i cannot get my output as followings. 

結果:私のオプションを指定できますどのような

array( 
[0]=>"This", 
[1]=>"is", 
[2]=>"a", 
[3]=>"test", 
[4]=>"from", 
[5]=>"php", 
[6]=>",", 
[7]=>"python", 
[8]=>",", 
[9]=>"asp", 
[10]=>"and", 
[11]=>"also", 
[12]=>"from", 
[13]=>"other", 
[14]=>"languages", 
[15]=>".", 
[16]=>"Alash", 
[17]=>"!", 
[18]=>"I", 
[19]=>"cannot", 
[20]=>"get", 
... 
) 

それはPHPで?

答えて

2

ような何か試してみてください:あなたはうわー、それはだ

$res = preg_split('/ |([.,])/' , $string,-1, PREG_SPLIT_DELIM_CAPTURE| PREG_SPLIT_NO_EMPTY); 
+0

は.../\ + S 'を使用するには余りにも – KoolKabin

+1

より良いスペースをスキップしていただければ幸い爆発使って、この方法を試してみてください| \ bの(?= \ W)/'、そしてあなたそれらの空の文字列で終わらないでください。 – mhyfritz

+0

gr8 one ... thnx働いた – KoolKabin

0

を難しいもの!あなたは "、"も保持したいので。

$string = "I beg to differ, you can get it as the previous."; 
$words = preg_split('/\s+|(?<=[,\.!\?])|(?=[,\.!\?])/',$string); 

注:ここで何をするかである(?<=)にと(?=)に、あなたはあなたが前にスペースがない場合でも、同様の言葉として考えることにしたいすべての文字を置く必要があり、および/またはそれらの後。

1

として区切り文字で機能を爆発使用することができます

preg_split('/\s+|\b/', $string) 
+0

申し訳ありませんが、期待通りに機能しませんでした。 – KoolKabin

+0

編集したものを試しましたか? – Shameer

2

何かのGR8だ

function multiexplode ($delimiters,$string) 
{ 

    $ready = str_replace($delimiters, $delimiters[0], $string); 
    $launch = explode($delimiters[0], $ready); 
    return $launch; 
} 

$text = "here is a sample: this text, and this will be exploded. this also | this one too :)"; 

$exploded = multiexplode(array(",",".","|",":"),$text); 

print_r($exploded); 
関連する問題