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使用redis消息队列发布微博的方法示例

本文实例讲述了PHP使用redis消息队列发布微博的方法。分享给大家供大家参考,具体如下: 在一些用户发布内容应用中,可能出现1秒上万个用户同时发布消息的情况,此时使用mysql可能会出...

PHP中的命名空间相关概念浅析

1. PHP中的命名空间是什么? 什么是命名空间?“从广义上来说,命名空间是一种封装事物的方法。在很多地方都可以见到这种抽象概念。例如,在操作系统中目录用来将相关文件分组,对于目录中的文...

PHP使用phpunit进行单元测试示例

本文实例讲述了PHP使用phpunit进行单元测试。分享给大家供大家参考,具体如下: 1. linux服务器上安装phpunit wget https://phar.phpunit....

php实现的AES加密类定义与用法示例

本文实例讲述了php实现的AES加密类定义与用法。分享给大家供大家参考,具体如下: CryptAES.class.php文件: <?php class CryptAES...

php逐行读取txt文件写入数组的方法 原创

本文实例讲述了php逐行读取txt文件写入数组的方法。分享给大家供大家参考。具体如下: 假设有user.txt文件如下: user01 user02 user03 user04 us...