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第一次无法获取cookie问题处理

php第一次无法获取cookie问题处理

首先编写以下简单的代码: 复制代码 代码如下: <?php     setcookie('a','value');   ...

PHP单例模式简单用法示例

本文实例讲述了PHP单例模式用法。分享给大家供大家参考,具体如下: <?php class db { public $conn; public static $...

PHP中__FILE__、dirname与basename用法实例分析

本文实例讲述了PHP中__FILE__、dirname与basename用法。分享给大家供大家参考。具体方法如下: 在php中__FILE__当前运行文件的完整路径和文件名,如果用在被包...

php实现xml与json之间的相互转换功能实例

本文实例讲述了php实现xml与json之间的相互转换功能。分享给大家供大家参考,具体如下: 用php实现xml与json之间的相互转换: 相关函数请查看php手册。 一、参考xml如下...

PHP模糊查询技术实例分析【附源码下载】

PHP模糊查询技术实例分析【附源码下载】

本文实例讲述了PHP模糊查询技术。分享给大家供大家参考,具体如下: 简介 从本质上揭密PHP模糊查询技术 功能 根据输入的关键字查找相关用户 PHP用户查询器案例分析...