PHP获取字符串的前5个单词的最佳方法是什么? [英] PHP What is the best way to get the first 5 words of a string?
本文介绍了PHP获取字符串的前5个单词的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
获取字符串的前5个单词的最佳方法是什么? 如何将字符串分成两部分,使得第一个子字符串具有原始字符串的前5个单词,第二个子字符串构成原始字符串的其余部分
What is the best way to get the first 5 words of a string? How can I split the string into two in such a way that first substring has the first 5 words of the original string and the second substring constitutes the rest of the original string
推荐答案
$pieces = explode(" ", $inputstring);
$first_part = implode(" ", array_splice($pieces, 0, 5));
$other_part = implode(" ", array_splice($pieces, 5));
explode
将原始字符串分成单词数组,array_splice
让您获得这些单词的特定范围,然后implode
将范围重新组合成单个字符串.
explode
breaks the original string into an array of words, array_splice
lets you get certain ranges of those words, and then implode
combines the ranges back together into single strings.
这篇关于PHP获取字符串的前5个单词的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文