PHP获取字符串的前5个单词的最佳方法是什么? [英] PHP What is the best way to get the first 5 words of a string?

查看:149
本文介绍了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆