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常用数学函数。分享给大家供大家参考。具体分析如下: abs()函数定义和用法: 返回一个数的绝对值. 语法:abs(x),代码如下: 复制代码 代码如下:$ab...

PHP中类的继承和用法实例分析

本文实例讲述了PHP中类的继承和用法。分享给大家供大家参考,具体如下: 1、继承关键字 :extends PHP类的继承,我们可以理解成共享被继承类的内容。PHP中使用extends单一...

用php解析html的实现代码

最近想用php写一个爬虫,就需要解析html,在sourceforge上找到一个项目叫做PHP Simple HTML DOM Parser,它可以以类似jQuery的方式通过css选择...

PHP 柱状图实现代码

PHP 柱状图实现代码

还有疑问的朋友可以加我QQ:460634320,大家一起讨论。 效果图:复制代码 代码如下: <?php function createImage($data,$twidth,$t...

PHP入门教程之上传文件实例详解

本文实例讲述了PHP上传文件的方法。分享给大家供大家参考,具体如下: Demo1.php <form enctype="multipart/form-data" action=...