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文件的上传和下载的思路差不多一样.也就是在代码中多了一个header语句 以下是详细的代码.仅供参考. 入口文件 复制代码 代码如下: <html> <b...

实现dedecms全站URL静态化改造的代码

转自bbs.dedecms.com 1、将include复制到网站中的include目录。 2、修改数据库 将所有文档设置为“仅动态”,可以进入数据库管理中,执行下面命令: update...

PHP基于自定义函数实现的汉字转拼音功能实例

本文实例讲述了PHP基于自定义函数实现的汉字转拼音功能。分享给大家供大家参考,具体如下: 整个过程用到了pinyin.table文件。 pinyin.php <?php...

PHP编程获取图片的主色调的方法【基于Imagick扩展】

本文实例讲述了PHP编程获取图片的主色调的方法。分享给大家供大家参考,具体如下: 代码中用到了php的图片扩展,所以使用之前,需要先安装PHP的Imagick扩展,具体安装如下(wind...

PHP array 的加法操作代码

The + operator appends elements of remaining keys from the right handed array to the left han...