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多线程类及用法。分享给大家供大家参考。具体分析如下: 一般来说可通过WEB服务器来实现PHP多线程功能,当然,对多线程有深入理解的人都知道通过WEB服务器实现的多线程...

PHP封装的PDO数据库操作类实例

本文实例讲述了PHP封装的PDO数据库操作类。分享给大家供大家参考,具体如下: <?php class DatabaseHandler { /** *...

PHP动态柱状图实现方法

PHP动态柱状图实现方法

本文实例讲述了PHP动态柱状图实现方法。分享给大家供大家参考。具体分析如下: 1.需求 查询最近一个月的数据总条数和审核通过的条数,做成柱状图 2.实现代码: <!DOCTY...

PHP+AJAX实现无刷新注册(带用户名实时检测)

很多时候,我们在网上注册个人信息,在提交完页面后,总得等待页面刷新来告诉我们注册是否成功,遇到网络差的时候,如果注册了一大串的东西,在经过漫长的等待页面刷新后,得到的确是“您的用户名已被使用”或XXX...

PHP抽象类与接口的区别详解

对于面向对象开发,抽象类与接口这两个东西是比较难理解的;就算是对于有一定经验的程序员来说也如此。下面根据自己的理解来讲述一下这两个东西,如有什么不对的,还望不吝赐教。 抽象类:是基于类来...