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简单操作MongoDB的方法(安装及增删改查)

PHP简单操作MongoDB的方法(安装及增删改查)

本文实例讲述了PHP简单操作MongoDB的方法。分享给大家供大家参考,具体如下: php操作MongoDB的话首先从网上下载MongoDB的扩展包,https://github.com...

thinkphp框架下404页面设置 仅三步

404页面即系统在找不到请求的操作方法和找不到请求的控制器名称时的一种报错行为的优化。 在很多网站中都会有使用404页面的时候,在ThinkPHP框架中该如何设置呢,接下来我介绍其中一种...

php创建多级目录与级联删除文件的方法示例

本文实例讲述了php创建多级目录与级联删除文件的方法。分享给大家供大家参考,具体如下: 创建多级目录 mkdir函数只能创建一级的目录,如果我们想创建多级目录,则需要自己编写函数。...

PHP 错误之引号中使用变量

当看到错误提示 syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE...

php 301转向实现代码

301转向定义 301转向(或叫301重定向,301跳转)是当用户或搜索引擎向网站服务器发出浏览请求时,服务器返回的HTTP数据流中头信息(header)中的状态码的一种,表示本网页永久...