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+jquery+ajax实现即时聊天功能实例

本文实例讲述了PHP+jquery+ajax实现即时聊天功能的方法。分享给大家供大家参考。具体如下: 这是一个简单的利用jquery与php做的一个聊天室的源码,我们这里定时利用ajax...

php限制ip地址范围的方法

本文实例讲述了php限制ip地址范围的方法。分享给大家供大家参考。具体如下: 只有在限定范围内的ip地址才能访问 function get_real_ipaddress() { i...

php 需要掌握的东西 不做浮躁的人

没有什么不同,下面的内容,就是针对一个Java程序员掌握的技能对比PHP来说的!告诉你,它们其实是一样的,不过是工具而已,没有高低贵贱之分。 只不过你不熟练,不知道而已! 1、语法:必须...

PHP获得当日零点时间戳的方法分析

本文实例讲述了PHP获得当日零点时间戳的方法。分享给大家供大家参考,具体如下: 今天项目中,想每天看到的是当天的全部信息,所以想获得当天零点的时间戳,复习下时间戳的相关知识,总结如下:...

php blowfish加密解密算法

PHP Blowfish 算法的加密解密,供大家参考,具体内容如下 <?php /** * php blowfish 算法 * Class blowfish...