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.

相关文章

smarty的保留变量问题

以下是访问页面请求变量诸如get,post,cookies,server,enviroment和session变量的例子. 例如{$smarty.server.SERVER_NAME}取...

PHP 读取和修改大文件的某行内容的代码

复制代码 代码如下: $fp = fopen('d:/file.txt', 'r+'); if ($fp) { $i = 1; while (!feof($fp)) { //修改第二行数...

php截取指定2个字符之间字符串的方法

本文实例讲述了php截取指定2个字符之间字符串的方法。分享给大家供大家参考。具体如下: 在php中只要判断字符串1与字符串2之前的一个stripos位置然后再使用substr开始截取就可...

PHP实现将颜色hex值转换成rgb的方法

本文实例讲述了PHP实现将颜色hex值转换成rgb的方法。分享给大家供大家参考,具体如下: function hex2rgb( $colour ) { if ( $colo...

php中addslashes函数与sql防注入

本文实例讲述了php中addslashes函数与sql防注入。分享给大家供大家参考。具体分析如下: addslashes可会自动给单引号,双引号增加\\\\\\,这样我们就可以安全的把数...