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.

相关文章

微信公众平台消息接口校验与消息接口响应实例

本文实例讲述了微信公众平台消息接口校验与消息接口响应的方法。分享给大家供大家参考。具体分析如下: 开发微信公众平台消息接口过程中,我们首先需要验证消息接口的有效性,验证通过后,才可以进行...

使用eAccelerator加密PHP程序

使用 eAccelerator 加密PHP程序 复制代码 代码如下:# /usr/local/bin/encoder 执行后会看到简单的使用说明: 复制代码 代码如下:Usage: en...

PHP中ini_set和ini_get函数的用法小结

php中的ini_set函数是php自带的用来修改设置php.ini配置文件的函数,用这个函数很方便,不用去手动修改php.ini文件,有时候我们也没有权限去修改php.ini文件,这时...

php 将字符串按大写字母分隔成字符串数组

alert("createTechBook".split(/(?=[A-Z])/)) 谢了啊 复制代码 代码如下: <?php $str="abcDefGhi"; /* preg_...

微信公众平台接口开发入门示例

本文实例讲述了微信公众平台接口开发入门示例。分享给大家供大家参考。具体如下: 微信公众平台的接口开发是一个现在比较常用的功能了,很多的人都会去了解一下微信公众平台一些简单开发应用,这里就...