2017-05-31 63 views
0

我真的需要幫助。我很抱歉,因爲我是PHP編碼的初學者。 我想剪切一個句子中的每個單詞,並列出每個單詞的索引。如何將句子切分成單詞並列出每個單詞的索引?

,例句:

I want buy a new shoes this weekend. 

我想這樣我的輸出:

[0] I , [1] want, [2] buy, [3] a, [4] new, [5] shoes, [6] this, [7] weekend 

我將如何在PHP中做到這一點?

謝謝。

+0

歡迎的StackOverflow!你到目前爲止嘗試過什麼嗎? StackOverflow不是一個免費的代碼寫入服務,並期望你[**嘗試首先解決你自己的問題**](http://meta.stackoverflow.com/questions/261592)。請更新您的問題以顯示您已經嘗試的內容,在[**最小,完整和可驗證的示例**](http://stackoverflow.com/help/mcve)中展示您面臨的特定問題。有關詳細信息,請參閱[**如何提出良好問題**](http://stackoverflow.com/help/how-to-ask),並參加[**遊覽**](http://該網站:) –

+1

請檢查這個PHP函數http://php.net/manual/en/function.explode.php –

回答

1

我希望這回答你的問題

print_r(explode(" ", "I want buy a new shoes this weekend.")); 

Array 
(
    [0] => I 
    [1] => want 
    [2] => buy 
    [3] => a 
    [4] => new 
    [5] => shoes 
    [6] => this 
    [7] => weekend. 
) 
1

您可以使用PHP的分裂()

$text = "I want buy a new shoes this weekend"; 
$words = explode(" ", $text); 
print_r($words); 

這會給下面的輸出。

Array 
(
[0] => I 
[1] => want 
[2] => buy 
[3] => a 
[4] => new 
[5] => shoes 
[6] => this 
[7] => weekend 

相關問題