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下统计用户在线时间的一种尝试

下面列出几个比较常用的方法: 首先介绍一下所涉及的数据表结构,四个字段: 复制代码 代码如下: uid<int(10)> :用户id session_id<varcha...

PHP中“简单工厂模式”实例代码讲解

PHP中“简单工厂模式”实例代码讲解

简单工厂模式: ①抽象基类:类中定义抽象一些方法,用以在子类中实现 ②继承自抽象基类的子类:实现基类中的抽象方法 ③工厂类:用以实例化对象 看完文章再回头来看下这张图,效果会比较好 采...

PHP之sprintf函数用法详解

本文实例讲述了PHP中sprintf函数的用法。分享给大家供大家参考。具体用法分析如下: sprintf()函数在php官方是说把字符串格式化输出了,本文就来给各位朋友介绍一下在学习sp...

PHP基于简单递归函数求一个数阶乘的方法示例

PHP基于简单递归函数求一个数阶乘的方法示例

本文实例讲述了PHP基于简单递归函数求一个数阶乘的方法。分享给大家供大家参考,具体如下: 一、问题: 求一个数a的阶乘,那么,a!=a*(a-1)*(a-2)*(a-3)*……*2*1....

自动把纯文本转换成Web页面的php代码

首先让我们来看一个我朋友希望转换的纯文本文件的例子: 以下为引用的内容: 复制代码 代码如下:   Green for Mars!   John R. Doe   The idea of...