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做推送服务端实现ios消息推送

准备工作1.获取手机注册应用的deviceToken(iphone手机注册应用时返回唯一值deviceToken)2.获取ck.pem文件(做手机端的给)3.获取pass phrase(...

php强制下载文件函数

本文实例为大家分享了php强制下载文件函数,供大家参考,具体内容如下 public function down() { $id = $this->_get('id');...

图文详解phpstorm配置Xdebug进行调试PHP教程

图文详解phpstorm配置Xdebug进行调试PHP教程

phpstorm配置Xdebug进行调试PHP教程分享给大家,具体内容如下 运行环境: PHPSTORM版本 : 8.0.1 PHP版本 : 5.6.2 xdebug版本:php_xde...

PHP 处理图片的类实现代码

复制代码 代码如下: <?php /** * author:yagas * email:yagas60@21cn.com */ class Image { /** 类保护变量 */...

php创建session的方法实例详解

php创建session的方法实例详解

本文实例讲述了php创建session的方法。分享给大家供大家参考。具体分析如下: 保存session只需要两个步骤,开启session和保存session数据。默认情况下,sessio...