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// 欄位字串為$querys = array("name"=>"shian");// 數值等於多少$querys = array("numbe...

支持汉转拼和拼音分词的PHP中文工具类ChineseUtil

PHP 中文工具类,支持汉字转拼音、拼音分词、简繁互转。 PHP Chinese Tool class, support Chinese pinyin, pinyin participl...

php实现等比例压缩图片

本文实例为大家分享了php实现等比例压缩图片的具体代码,供大家参考,具体内容如下 /** * desription 压缩图片 * @param sting $imgsrc...

PHP5全版本绕过open_basedir读文件脚本漏洞详细介绍

PHP5全版本绕过open_basedir读文件脚本漏洞详细介绍

漏洞很久之前(大概5年前)被提出来了,但并不是php代码上的问题,所以问题一直存在,直到现在。我一直没留意,后来yaseng告诉我的,他测试了好像5.5都可以。 漏洞详情在这里 http...

用PHP来计算某个目录大小的方法

PHP CURL session COOKIE 可以调用系统命令,还可以这样: 复制代码 代码如下: function dirsize($dir) { @$dh = opendir($d...