0

假设我有一个文本Hello Himanshu How are you, Hope all well

我已将其存储在一个变量$content中,即$content= "Hello Himanshu How are you, Hope all well".

我想将第一个单词存储在 1 个变量中,第二个单词存储在第二个变量中,其余的文本存储在第三个变量中。

我在 PHP 中使用了explode 函数将前两个存储在单独的变量中,但我不知道如何将其余字符串存储在单个变量中。

$arr=explode(' ',trim($content));

$word1=$arr[0];

$word2=$arr[1];

$rest_words=$arr[2];

期望的输出:

$word1 = "你好"

$word2 =“喜满树”

$rest_words = "你好吗,希望一切顺利"

4

1 回答 1

2

您可以使用

$arr=explode(' ',trim($content),3);

检查PHP爆炸手册limit的部分

$content= "Hello Himanshu How are you, Hope all well";
$arr=explode(' ',trim($content),3);
$word1=$arr[0];
$word2=$arr[1];
$rest_words=$arr[2];

echo $word1, "\n", $word2, "\n", $rest_words;
于 2013-08-03T15:50:34.800 回答