【发布时间】:2013-09-20 00:25:23
【问题描述】:
所以基本上我硬了一个非常大的字符串,我只想保存它的前 4 个单词。
我几乎可以做到这一点,尽管有些情况会破坏它。
这是我当前的代码:
$title = "blah blah blah, long paragraph goes here";
//Make title only have first 4 words
$pieces = explode(" ", $title);
$first_part = implode(" ", array_splice($pieces, 0, 4));
$title = $first_part;
//title now has first 4 words
破坏它的主要案例是line-breaks。如果我有这样的段落:
Testing one two three
Testing2 a little more three two one
$title 将等于 Testing one two three Testing2
另一个例子:
Testing
test1
test2
test3
test4
test5
test6
sdfgasfgasfg fdgadfgafg fg
标题等于 = Testing test1 test2 test3 test4 test5 test6 sdfgasfgasfg fdgadfgafg fg
由于某种原因,它正在抓取下一行的第一个单词。
有人对如何解决这个问题有任何建议吗?
【问题讨论】:
标签: php string explode implode