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实现的登录,注册及密码修改功能分析

PHP实现的登录,注册及密码修改功能分析

本文实例讲述了PHP实现登录,注册及密码修改功能的方法。分享给大家供大家参考,具体如下: 这里介绍注册,登录,修改密码的界面布局与功能实现: 1.登录 2.忘记密码 3.免费注册...

PHP下操作Linux消息队列完成进程间通信的方法

关于Linux系统进程通信的概念及实现可查看:http://www.ibm.com/developerworks/cn/linux/l-ipc/   关于Linux系统消息队列的概念及实...

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

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

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

PHP的cookie与session原理及用法详解

PHP的cookie与session原理及用法详解

本文实例讲述了PHP的cookie与session原理及用法。分享给大家供大家参考,具体如下: 产生背景 HTTP协议是无状态的协议。一旦数据交换完毕,客户端与服务器端的连接就会关闭,再...

简单谈谈PHP中的Reload操作

前言 有很多前辈告诫过我们,reload 能保证整个过程的平滑性,所谓平滑性指的是在 reload 的过程中,旧的进程在处理完当前请求前不会提前终止。很多年来,我从来没有质疑过这种说法,...