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

yipeiwu_com6年前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中常见的sql攻击正则表达式汇总

本文实例讲述了php中常见的sql攻击正则表达式。分享给大家供大家参考。具体分析如下: 我们都已经知道,在MYSQL 5+中 information_schema库中存储了所有的 库名,...

PHP文件锁函数flock()详细介绍

文件操作系统是在网络环境下完成的,可能有多个客户端用户在同一个时刻对服务器上的同一个文件访问。当这种并发访问产生时,很可能会破坏文件中。例如一个用户正在向文件中写入数据,当还没有写完时,...

php 判断访客是否为搜索引擎蜘蛛的函数代码

复制代码 代码如下: /** * 判断是否为搜索引擎蜘蛛 * * @author Eddy * @return bool */ function isCrawler() { $agent...

PHP简单实现断点续传下载的方法

本文实例讲述了PHP实现断点续传下载的方法。分享给大家供大家参考。具体如下: $fname = 'http://XXXX/MMLDZG.mp3'; $fp = fopen($fnam...

PHP去除空数组且数组键名重置的讲解

php空数组的话,能够运用 php函数array_filter() . array array_filter ( array [, callback callback] ) ar...