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中,我们都知道有专门的mb_substr和mb_strlen函数,可以对中文进行截取和计算长度,但是,由于这些函数并非PHP的核心函数,所以,它们常常有可能没有开启。当然,如果是...

解决FastCGI 进程超过了配置的活动超时时限的问题

解决FastCGI 进程超过了配置的活动超时时限的问题

近日,需要满足测试需求,进行大数据并发测试时,报出【HTTP 错误 500.0 - Internal Server Error E:\PHP\php-cgi.exe - FastCGI...

PHP使用递归算法无限遍历数组示例

本文实例讲述了PHP使用递归算法无限遍历数组。分享给大家供大家参考,具体如下: (PS:为方便阅读,此处代码使用php代码格式化工具http://tools.jb51.net/code/...

PHP实现获取毫秒时间戳的方法【使用microtime()函数】

本文实例讲述了PHP实现获取毫秒时间戳的方法。分享给大家供大家参考,具体如下: PHP获取毫秒时间戳,利用microtime()函数 php本身没有提供返回毫秒数的函数,但提供了一个mi...

PHP7.1新功能之Nullable Type用法分析

本文实例分析了PHP7.1新功能之Nullable Type用法。分享给大家供大家参考,具体如下: 在 PHP5 时代,PHP 的参数已经支持 type hint(除了基本类型),想必大...