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获取linux命令结果的实例

如果使用php命令行里想获取etho网卡的IP怎么处理呢 ? public function get_server_ip() { if (PHP_SAPI === 'cli')...

Zend Framework中的简单工厂模式 图文

Zend Framework中的简单工厂模式 图文

前段时间用来ZF,把他当黑盒感觉不是很好,一直有看其源码的冲动,但是。。。如果一点一点点看的话,挑战确实有些大了。某天又然后想到好久没复习设计模式了。综合一下,复习一个设计模式之后在ZF...

PHP单文件上传原理及上传函数的封装操作示例

本文实例讲述了PHP单文件上传原理及上传函数的封装操作。分享给大家供大家参考,具体如下: 表单: 0.php: <!doctype html> <html>...

PHP Zip解压 文件在线解压缩的函数代码

复制代码 代码如下: /********************** *@file - path to zip file *@destination - destination dire...

PHP中if和or运行效率对比

本文实例讲述了PHP中if和or运行效率对比。分享给大家供大家参考。具体实现方法如下: 对if和or的运行效率进行了实例说明,感兴趣的朋友可以测试一下,这里我测试了的结果是or 比if效...