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桥接模式应用案例分析

本文实例讲述了php桥接模式应用。分享给大家供大家参考,具体如下: 桥接模式是将抽象部分与它的实现部分分离,使它们都可以独立地变化。 示例: 当一个信息时 根据发送渠道分为:QQ消息、e...

php查询ip所在地的方法

本文实例讲述了php查询ip所在地的方法。分享给大家供大家参考。 具体实现方法如下: 复制代码 代码如下:<?php /** *@ date  &n...

PHP正则匹配中英文、数字及下划线的方法【用户名验证】 原创

本文实例讲述了PHP正则匹配中英文、数字及下划线的方法。分享给大家供大家参考,具体如下: 一、问题: 对于用户注册时的用户名要求由中英文、数字或下划线组成,不得含有其他字符。 二、解决方...

解析php中array_merge与array+array的区别

array_merge是丢弃原来的数字的key,而保留字符串形式的key,然后组成一个新的数组,不管键名是否一样,都不合并,除非键名和value同时一样并且还必须是字符串形式的key才合...

PHP图像处理之imagecreate、imagedestroy函数介绍

使用PHP的GD库处理图像时,必须对画布进行管理。创建画布就是在内存中开辟一块存储区域,以后在PHP中对图像的所有操作都是基于这个图布处理的,图布就是一个图像资源。在PHP中,可以使用i...