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代码简化

复制代码 代码如下:<? echo("<p>search results for query:"). $_GET['query'].".</p>"; ?&g...

PHP判断是否连接上网络的方法

本文实例讲述了PHP判断是否连接上网络的方法。分享给大家供大家参考。具体实现方法如下: 首先写个函数 function varify_url($url){ $check = @fo...

修改php.ini不生效问题解决方法(上传大于8M的文件)

摘要:上传大于8M的文件需要修改php的配置才可以生效。但是我在网上找了一堆修改配置的资料,但是自己修改之后就是没有生效。 解决方法: 修改php.ini这个选项,网上有很多的教程,可以...

PHP魔术引号所带来的安全问题分析

PHP通过提取魔术引号产生的“\”字符会带来一定的安全问题,例如下面这段代码片段: // foo.php?xigr='ryat function daddslashes($s...

PHP中return 和 exit 、break和contiue 区别与用法

先说一下exit函数的用法。 作用: 输出一则消息并且终止当前脚本。 如果一段文本中包括多个以 结束的脚本,则exit退出当前所在脚本。 比如一篇php文本包括一下代码,则输出为worl...