php的字符串用法小结

yipeiwu_com5年前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完全二叉树定义与实现方法示例

本文实例讲述了PHP完全二叉树定义与实现方法。分享给大家供大家参考,具体如下: 若设二叉树的深度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结...

php仿微信红包分配算法的实现方法

本文实例讲述了php仿微信红包分配算法的实现方法。分享给大家供大家参考,具体如下: /** * 红包分配:把一定金额随机分配给指定人数 * * @param int $mone...

php类的扩展和继承用法实例

本文实例讲述了php类的扩展和继承用法。分享给大家供大家参考。具体如下: <?php class Thread { var $topic; //帖子主题...

PHP实现生成模糊图片的方法示例

PHP实现生成模糊图片的方法示例

本文实例讲述了PHP实现生成模糊图片的方法。分享给大家供大家参考,具体如下: <?php class image_blur{ /** * 图片高斯模糊(适用于png...

php获取后台Job管理的实现代码

复制代码 代码如下: <?php defined('SYSPATH') OR die('No direct access allowed.'); class Controller_...