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实现二分查找算法代码分享

第一种方法: 【二分查找要求】:1.必须采用顺序存储结构 2.必须按关键字大小有序排列。    【优缺点】折半查找法的优点是比较次数少,查找速度快,平均性能好;其缺点是要求待查表为有序表...

PHP字符串中抽取子串操作实例分析

本文实例讲述了PHP字符串中抽取子串操作。分享给大家供大家参考,具体如下: 问题 希望从字符串的某个特定位置开始抽取这个字符串的一部分。例如,对于输入到一个表单的用户名,想要得到这个用...

详谈PHP面向对象中常用的关键字和魔术方法

PHP面向对象中常用的关键字    final 1.final不能修饰成员属性(类中常量不是用这个关键字) 2.final只能修饰类和方法 作用: 使用fi...

一个简单且很好用的php分页类

复制代码 代码如下:class Page {    // 分页栏每页显示的页数    public $rollPage = 6...

使用PHP计算两个路径的相对路径

复制代码 代码如下:<html>     <body>     &nbs...