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.

相关文章

关于Appserv无法打开localhost问题的解决方法

安装了Appserv时,无法打开http://localhost或是http://127.0.0.1 在端口没有被占的情况下(本来我就没安装IIS),只要启动下D:\AppServ\Ap...

利用php操作memcache缓存的基础方法示例

前言 最近在工作中又遇到了memcache,大家应该都有所了解,memcache 是一个高效的分布式的内存对象缓存系统,他可以支持把php的各种数据(数组,对象,基本数据类型)放在它管理...

在WordPress中使用wp_count_posts函数来统计文章数量

做一个全站统计是不是很酷?长久的博客越来越少,何不给自己的一个统计,看看自己在这个博客上努力了多少,不但给自己也给游客,wp_count_posts是在 WordPress 中用来统计文...

PHP数组函数array_multisort()用法实例分析

本文实例分析了PHP数组函数array_multisort()用法。分享给大家供大家参考,具体如下: 有时候我们需要对二维数组的某个键的值进行排序,这里就是讨论这个问题。我们可以使用ar...

PHP表单验证的3个函数ISSET()、empty()、is_numeric()的使用方法

ISSET();——适合于检测是否存在这个参数。 定义和作用范围:用于测试一个变量是否具有值(包括0,FALSE,或者一个空字串,但不能是NULL),即:“http://localhos...