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数组比较实现查找连续数的方法。分享给大家供大家参考。具体如下: $data = array(); $data[]= array("01" ,"02", "18"...

php中判断数组相等的方法以及数组运算符介绍

php中判断数组相等的方法以及数组运算符介绍

如何判断两个数组相等呢?其实很简单,用 == 或者 === 就可以了 php手册里说明如下: 那像 array('k'=>array())这样的多维数组能用如上方法判断相等吗?当...

PHP实现的随机IP函数【国内IP段】

本文实例讲述了PHP实现的随机IP函数。分享给大家供大家参考,具体如下: function get_rand_ip(){ $arr_1 = array("218","218","...

PHP CKEditor 上传图片实现代码

我花了一个下午的时间,自己用PHP脚本写了一个处理上传文件的脚本代码,没有做更多的安全处理,希望对大家有用。 首先,在你的config.js文件里添加如下代码: 复制代码 代码如下: C...

PHP基于递归算法解决兔子生兔子问题

本文实例讲述了PHP基于递归算法解决兔子生兔子问题。分享给大家供大家参考,具体如下: 接到面试通知辗转反侧,一直在默念明天改如何介绍自己的项目经验等。 早早的起床,洗漱,把自己的总结的问...