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使用fgetcsv读取csv文件出现乱码的解决方法

本文实例讲述了php使用fgetcsv读取csv文件出现乱码的解决方法。分享给大家供大家参考。具体分析如下: 一般来说在php中碰到乱码多半是编码问题,在这里我们实例分析了fgetcsv...

dedecms中显示数字验证码的修改方法

输入字母验证码,俺觉得特烦,特别还要输入大写字母。于是找到文件并修改成数字验证码。 修改文件 验证码文件位置 include\validateimg.php 找到 for($i...

PHP中的Iterator迭代对象属性详解

PHP中的Iterator迭代对象属性详解

前言 foreach用法和之前的数组遍历是一样的,只不过这里遍历的key是属性名,value是属性值。在类外部遍历时,只能遍历到public属性的,因为其它的都是受保护的,类外部不可见。...

php使用正则表达式获取图片url的方法

本文实例讲述了php使用正则表达式获取图片url的方法。分享给大家供大家参考。 具体实现方法如下: 复制代码 代码如下: <?php header("Content-typ...

PHP序列号生成函数和字符串替换函数代码

复制代码 代码如下: /** * 序列号生成器 */ function snMaker($pre = '') { $date = date('Ymd'); $rand = rand(10...