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.

相关文章

解析二进制流接口应用实例 pack、unpack、ord 函数使用方法

在工作中,我也逐渐了解到pack,unpack,ord对于二进制字节处理的强大。 下面我逐一介绍它们。在我们工作中,用到它们的估计不多。 我在最近一个工作中,因为通讯需要用到二进制流,然...

PHP查询快递信息的方法

本文实例讲述了PHP查询快递信息的方法。分享给大家供大家参考。具体如下: 这里使用快递100物流查询 官方文档中只能返回html的接口 也可以返回json php代码如下: 复制代码 代...

php实现与erlang的二进制通讯实例解析

一般来说网络通讯常用的方式有2种:文本通讯和二进制通讯。php与erlang之间实现文本通讯比较简单,这里就不做讨论,本文主要讨论的是php与erlang实现二进制通讯的实现方法。实现步...

phpMyAdmin 链接表的附加功能尚未激活问题的解决方法(已测)

phpMyAdmin 链接表的附加功能尚未激活问题的解决方法(已测)

从多方查找资源并测试终于解决了问题,特记录下。其实如果在安装的时候,正确的安装,就不会出现下面的信息了. 如在Linux下安装,提示密码的时候,输入Root账号的密码就不会出现这个问题了...

PHP类和对象相关系统函数与运算符小结

本文总结了PHP类和对象相关系统函数与运算符。分享给大家供大家参考,具体如下: 系统函数 class_exists() 判断某个类是否存在...