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.

相关文章

解析二进制流接口应用实例 pack、unpack、ord 函数使用方法

在工作中,我也逐渐了解到pack,unpack,ord对于二进制字节处理的强大。 下面我逐一介绍它们。在我们工作中,用到它们的估计不多。 我在最近一个工作中,因为通讯需要用到二进制流,然...

thinkphp查询,3.X 5.0方法(亲试可行)

一、介绍 ThinkPHP内置了非常灵活的查询方法,可以快速的进行数据查询操作,查询条件可以用于读取、更新和删除等操作,主要涉及到where方法等连贯操作即可,无论是采用什么数据库,你几...

php实现将二维关联数组转换成字符串的方法详解

本文实例讲述了php实现将二维关联数组转换成字符串的方法。分享给大家供大家参考,具体如下: 需求 项目中遇到了二维关联数组转字符串的问题,查阅相关资料,写了如下程序,并且能过滤重复的关键...

php给一组指定关键词添加span标签的方法

本文实例讲述了php给一组指定关键词添加span标签的方法。分享给大家供大家参考。具体如下: 这里是php给一组指定的关键词添加span标签,高亮突出显示关键词 // Example...

PHP 面向对象详解

对象的主要三个特性 对象的行为:可以对 对象施加那些操作,开灯,关灯就是行为。 对象的形态:当施加那些方法是对象如何响应,颜色,尺寸,外型。 对象的表示:对象的表示就相当于身份证,具体区...