php按单词截取字符串的方法

yipeiwu_com5年前PHP代码库

本文实例讲述了php按单词截取字符串的方法。分享给大家供大家参考。具体分析如下:

这里指定字符串和单词数量进行截取

复制代码 代码如下:
<?php
function limit_words($string, $word_limit)
{
    $words = explode(" ",$string);
    return implode(" ",array_splice($words,0,$word_limit));
}
//Example Usage
$content = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
echo limit_words($content,20);
?>

希望本文所述对大家的php程序设计有所帮助。

相关文章

PHP 源代码分析 Zend HashTable详解第1/3页

HashTable在通常的数据结构教材中也称作散列表,哈希表。其基本原理比较简单(如果你对其不熟悉,请查阅随便一本数据结构教材或在网上搜索),但PHP的实现有其独特的地方。理解了Hash...

php摘要生成函数(无乱码)

在使用的时候,得先把要生成摘要的内容strip_tags()一下,当然,你也可以把strip_tags()直接添加到函数中,我没有搞,自己添加吧。下面是函数: 复制代码 代码如下: fu...

php htmlspecialchars加强版

复制代码 代码如下: //取消HTML代码 function shtmlspecialchars($string) { if(is_array($string)) { foreach($...

PHP在特殊字符前加斜杠的实现代码

复制代码 代码如下: <?php $zongzi = "asdfasdf(asdfasdf?asfdadsf)"; echo $zongzi = quotemeta($zongzi...

fleaphp常用方法分页之Pager使用方法

Pager 分页函数 复制代码 代码如下: /** * 构造函数 * * 如果 $source 参数是一个 TableDataGateway 对象,则 FLEA_Helper_Pager...