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 url地址栏传中文乱码解决方法集合

php地址栏传中文$_GET下来后乱码,urlencode和urldecode用法详解url编码语法: string urlencode(string str);返回值: 字符串函数种类...

PHP中addslashes()和stripslashes()实现字符串转义和还原用法实例

本文实例讲述了PHP中addslashes()和stripslashes()实现字符串转义和还原用法。分享给大家供大家参考,具体如下: PHP中addslashes() 函数返回在预定义...

非常实用的php验证码类

本文实例为大家分享了php验证码类,供大家参考,具体内容如下 <?php /** * * @author Administrator * */ cl...

详解PHP的Yii框架的运行机制及其路由功能

详解PHP的Yii框架的运行机制及其路由功能

运行机制概述 每一次 Yii 应用开始处理 HTTP 请求时,它都会进行一个近似的流程。 用户提交指向 入口脚本 web/index.php 的请求。 入口脚本会加载 配置数组...

PHP里的$_GET数组介绍

在开发过程中,GET和POST无处不在。$_GET 变量是一个数组,内容是由 HTTP GET 方法发送的变量名称和值。 $_GET 变量用于收集来自 method="get" 的表单中...