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开发注意事项总结

1.使用内嵌的HTML代码,而不是PHP的echo语句。 因为PHP是一门嵌入式Web编程语言,可以将HTML代码和PHP代码相互嵌入。但是很多程序员担心在HTML代码中过多的使用”"嵌...

Zend Studio去除编辑器的语法警告设置方法

Zend Studio去除编辑器的语法警告设置方法

环境:Zend Studio 8.0 Zend Studio是PHP开发者的首选开发工具,其地位相当于微软开发工具中的Visual Studio。Zend Studio的编辑器可以帮我...

浅谈PHP封装CURL

浅谈PHP封装CURL

CURL是一个非常强大的开源库,支持很多协议,包括HTTP、FTP、TELNET等,我们使用它来发送HTTP请求。它给我 们带来的好处是可以通过灵活的选项设置不同的HTTP协议参数,并且...

PHP编码转换函数 自动转换字符集支持数组转换

复制代码 代码如下: // 自动转换字符集 支持数组转换 function auto_charset($fContents, $from='gbk', $to='utf-8') { $f...

PHP类的特性实例分析

本文实例讲述了PHP类的特性。分享给大家供大家参考,具体如下: 对象向下传递特性 当一个对象调用一个实例方法,然后在该方法中又去静态调用另一个类的方法,则在被静态调用的方法中获得源方法中...