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中ftp_chdir与ftp_cdup函数用法

本文实例讲述了php中ftp_chdir与ftp_cdup函数用法。分享给大家供大家参考。具体用法如下: ftp_chdir()函数 若成功,则返回 true,否则返回 false,如果...

PHP使用pdo连接access数据库并循环显示数据操作示例

本文实例讲述了PHP使用pdo连接access数据库并循环显示数据操作。分享给大家供大家参考,具体如下: PDO连接与查询: try { $conn = new PDO("odbc:...

php实现将二维关联数组转换成字符串的方法详解

本文实例讲述了php实现将二维关联数组转换成字符串的方法。分享给大家供大家参考,具体如下: 需求 项目中遇到了二维关联数组转字符串的问题,查阅相关资料,写了如下程序,并且能过滤重复的关键...

php将fileterms函数返回的结果变成可读的形式

复制代码 代码如下: function perms_str($perms){     if (($perms & 0xC000) == 0xC000) {  ...

PHP实现读取一个1G的文件大小

需求如下: 现有一个1G左右的日志文件,大约有500多万行, 用php返回最后几行的内容。 1. 直接采用file函数来操作 or file_get_content() 肯定报内存溢出注...