2017-05-31 64 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 

相关问题