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.

相关文章

PHPLog php 程序调试追踪工具

PHPLog php 程序调试追踪工具

原理:    1.程序执行的过程中,在相应的地方记录你想要追踪的变量及调用栈和每次函数调用的参数,        &...

获取远程文件大小的php函数

复制代码 代码如下:<?php function getFileSize($url){ $url = parse_url($url); if($fp = @fsockopen($u...

PHP预定义超全局数组变量小结

本文实例讲述了PHP预定义超全局数组变量。分享给大家供大家参考,具体如下: 1.PHP 预定义的超全局变量数组 特性: a.特殊的数组,操作方式没有区别 b.不用声明,php脚本中默认存...

PHPExcel合并与拆分单元格的方法

本文实例讲述了PHPExcel合并与拆分单元格的方法。分享给大家供大家参考,具体如下: $objPHPExcel; $filepath="c:\temp.xlsx"; try {...

Mac下php 5升级到php 7的步骤详解

前言 在MAC OS X 10.11中php的版本是5.5的,近来一年多里,看到了很多关于php7介绍,以为php7增加了很多新特性,也删除了原来很多的老特性,所以一直以来并没想去尝试使...