php的字符串用法小结

yipeiwu_com5年前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反射机制来构造"CREATE TABLE"的sql语句

反射是指在PHP运行状态中,扩展分析PHP程序,导出或提取出关于类、方法、属性、参数等的详细信息,包括注释。这种动态获取的信息以及动态调用对象的方法的功能称为反射API。反射是操纵面向对...

php使用pclzip类实现文件压缩的方法(附pclzip类下载地址)

本文实例讲述了php使用pclzip类实现文件压缩的方法。分享给大家供大家参考,具体如下: 使用PclZIp(zip格式)压缩,首先需要下载它的包文件(可点击此处本站下载)。PclZip...

由php if 想到的些问题

复制代码 代码如下:<?php  /* PHP code */  header("Content-type: text/...

使用PHP把HTML生成PDF文件的几个开源项目介绍

利用PHP编码生成PDF文件是一个非常耗时的工作。在早期,开发者使用PHP并借助FPDF来生成PDF文件。但是如今,已经有很多函数库可以使用了,并且能够从你提供的HTML文件生成PDF文...

php获取随机数组列表的方法

php获取随机数组列表的方法

本文实例讲述了php获取数组中随机数组的实例程序,分享给大家供大家参考。具体实现方法如下: 不用多说,直接贴代码,php中的array_rand很变态,突破了正常人的理解,非常繁琐 例1...