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 (http://www.wjgilmore.com)";
$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字符串与数组处理函数用法。分享给大家供大家参考,具体如下: 字符串处理函数 trim –去除字符串首尾的多余空白字符和其他字符 函数结构: string trim ( string $str [, string $character_mask...
本文实例分析了php字符串比较函数用法。分享给大家供大家参考,具体如下: 直接比较字符串是否完全一致,可以使用”==”来进行,但是有时候可能需要进行更加复杂的字符串比较,如部分匹配等. 1.strcmp()函数:该函数进行...
本文实例讲述了PHP常见数组函数用法。分享给大家供大家参考,具体如下: 1.array array_merge(array $array1 [, array $array2 [, $array]]) 函数功能:将一个或多个数组的单元合并起来,一个数组中的值附加在前一...
加入的这个函数的作用就是移除字符串开头和结尾的空白字符。 12.最后,如果输入错误了,重新回到登录对话框时,我们希望可以使用户名和 密码框清空并且光标自动跳转到用户名输入框,最终的登录按钮的单击事件的槽 ...
Linux 操作系统基础教程 清华大学信息学院计算机系 目 录 前言..........................................................................................................................................