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.

相关文章

自定义session存储机制避免会话保持问题

自定义session存储机制避免会话保持问题

PHP服务端session以文件的方式存储,当用户访问量过大时,session文件会非常多,而且当横向增加服务器后,session文件并不能同步,面临会话保持的问题。 有以下两种解决方案...

基于xcache的配置与使用详解

一、安装Xcache复制代码 代码如下:# wget http://xcache.lighttpd.net/pub/Releases/1.3.0/xcache-1.3.0.tar.gz#...

php安装php_rar扩展实现rar文件读取和解压的方法

本文实例讲述了php安装php_rar扩展实现rar文件读取和解压的方法。分享给大家供大家参考,具体如下: PHP Rar Archiving 模块 (php_rar) 是一个读取和解压...

PHP 定界符 使用技巧

如果用传统的输出方法——按字符串输出的话,肯定要有大量的转义符来对字符串中的引号等特殊字符进行转义,以免出现语法错误。如果是一两处还可以容忍,但是要是一个完整的html文本或者是一个20...

PHP中的多行字符串传递给JavaScript的两种方法

PHP和JavaScript都是初学。最近有这么个需求: 比方说有一个PHP的多行字符串: $a = <<<EOF thy38 csdn blog EOF...