php的字符串用法小结

yipeiwu_com5年前PHP代码库
1 求长度,最基本的
$text = "sunny day";
$count = strlen($text); // $count = 9


2 字符串截取
截取前多少个字符
$article = "BREAKING NEWS: In ultimate irony, man bites dog."; $summary = substr_replace($article, "...", 40);

3 算单词数
$article = "BREAKING NEWS: In ultimate irony, man bites dog."; $wordCount = str_word_count($article);
// $wordCount = 8

4 将字符串变成HTML的连接
$url = "W.J. Gilmore, LLC (//www.jb51.net)";
$url = preg_replace("/http://([A-z0-9./-]+)/", "$0", $url);

5 去除字符中的HTML字符串
$text = strip_tags($input, "
");

6 nl2br:
$comment = nl2br($comment);
变成带HTML格式

7 Wordwrap
限制每行字数
$speech = "Four score and seven years ago our fathers brought forth, upon this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.";
echo wordwrap($speech, 30);

输出:
Four score and seven years ago our fathers brought forth, upon this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.

相关文章

PHP has encountered a Stack overflow问题解决方法

昨晚将一个disucz论坛进行转移后,发现打开的页面上回多一个PHP has encountered a Stack overflow 这个提示错误,进过翻译为“PHP遇到堆栈溢出”。我...

PHP实现十进制、二进制、八进制和十六进制转换相关函数用法分析

本文实例讲述了PHP实现十进制、二进制、八进制和十六进制转换相关函数用法。分享给大家供大家参考,具体如下: 1.二进制: 1.1.二进制转十进制: 函数:bindec(string $b...

图文介绍PHP添加Redis模块及连接

图文介绍PHP添加Redis模块及连接

下面通过图文并茂的方式给大家展示如下: 上几篇文章介绍了Redis的安装及使用,下面将介绍php如何添加Redis扩展!   php手册并没有提供Redis的类和方法,也没有提供相关的扩...

PHP解压ZIP文件到指定文件夹的方法

本文实例讲述了PHP解压ZIP文件到指定文件夹的方法。分享给大家供大家参考,具体如下: /** * function: 解压zip 格式的文件 * author:friker...

php模板函数 正则实现代码

我看过phpcms、discuz的源码,所以可能就缺乏创新了,不过原理大都相通,只是细节处理可能稍微不同。 说正题,下面开始谈谈具体实现过程了。 1.首先要想好模板文件放在哪?转换后的p...