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实现统计网站在线人数的方法

本文实例讲述了php实现统计网站在线人数的方法。分享给大家供大家参考。具体实现方法如下: <?php function getIpAddress() { // 取得当前...

PHP基于socket实现的简单客户端和服务端通讯功能示例

本文实例讲述了PHP基于socket实现的简单客户端和服务端通讯功能。分享给大家供大家参考,具体如下: 服务器端: <?php set_time_limit(0);...

php selectradio和checkbox默认选择的实现方法详解

这是扩展yibing的select默认选择的实现方法 复制代码 代码如下:<select name="wuyeleixing" size="1"> <option &l...

php根据日期或时间戳获取星座信息和生肖等信息

php根据日期或时间戳获取星座信息和生肖等信息

分享一个利用php根据日期或时间戳获取相应的干支纪年,生肖和星座信息的函数方法,具体函数代码以及使用方法如下: /** 判断干支、生肖和星座 */ function birt...

比较时间段一与时间段二是否有交集的php函数

复制代码 代码如下: /* *比较时间段一与时间段二是否有交集 */ function isMixTime($begintime1,$endtime1,$begintime2,$endt...