欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php的字符串用法小结

程序员文章站 2022-04-19 09:31:27
1 求长度,最基本的 $text = "sunny day"; $count = strlen($text); // $count = 9 2 字符串截取 截取前多少个字符...
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.