php的字符串用法小结

yipeiwu_com6年前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简单统计在线人数的方法

本文实例讲述了php简单统计在线人数的方法。分享给大家供大家参考,具体如下: <?php //首先你要有读写文件的权限 //本程序可以直接运行,第一次报错,以缶涂梢&#...

thinkphp框架实现删除和批量删除

thinkphp框架实现删除和批量删除

本文实例讲一下如何用thinkphp实现数据的删除和批量删除吧。 预期效果图:   原谅博主对照片的处理是如此的草率吧。。。 仍然是 通过MVC模式进行拆分: 首先是视图部...

php面向对象中static静态属性与方法的内存位置分析

本文实例分析了php面向对象中static静态属性与方法的内存位置。分享给大家供大家参考。具体如下: static静态属性的内存位置——>类,而不是对象。下面做测试来证明一下...

php使用curl伪造浏览器访问操作示例

本文实例讲述了php使用curl伪造浏览器访问操作。分享给大家供大家参考,具体如下: 原理 服务器主要通过User-Agent识别客户端是何种设备 User-Agent是Http协议中的...

php延迟静态绑定实例分析

本文实例讲述了php延迟静态绑定的方法。分享给大家供大家参考。具体分析如下: php延迟静态绑定:指类的self,不是以定义时为准,而是以计算时的运行结果为准。先看一个实例 <...