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 ob_flush,flush在ie中缓冲无效的解决方法

PHP程序的缓冲,而不论PHP执行在何种情况下(CGI ,web服务器等等)。该函数将当前为止程序的所有输出发送到用户的浏览器。 flush() 函数不会对服务器或客户端浏览器的缓存模式...

mysq GBKl乱码

我现在有一个sql文件,里面内容是gbk的。我现在显示全是乱码。 就只是用mysql 4.0.26能显示。 用4.1以上的死活都显示不了。头大了。望大家指点指点,感恩不尽。 我...

php使用curl出现Expect:100-continue解决方法

本文实例讲述了php使用curl出现Expect:100-continue解决方法。分享给大家供大家参考。具体如下: 使用curl POST数据时,如果POST的数据大于1024字节,c...

PHP Ajax中文乱码问题解决方法

是因为XMLHttp在处理返回的responstText的时候把responstBody按UTF-8编码进行解码的,如果服务器端送出的数据流的确是UTF-8编码,那么中文字就会正确显示,...

详解cookie验证的php应用的一种SSO解决办法

详解cookie验证的php应用的一种SSO解决办法 近日,项目中需要接入一个“年久失修”的PHP应用,由于系统已经建设多年,并且是信息中心自己的人通过某些工具弄出来的,而且是本人未真正...