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操作MongoDB实现增删改查功能【附php7操作MongoDB方法】

本文实例讲述了PHP操作MongoDB实现增删改查功能。分享给大家供大家参考,具体如下: MongoDB的PHP驱动提供了一些核心类来操作MongoDB,总的来说MongoDB命令行中有...

PHP INT类型在内存中占字节详解

PHP INT类型在内存中占字节详解

本教程将介绍输出INT类型在内存中占多少个字节 新建一个333.php,如图所示: 添加php的界定符(<?php?>),如图所示: 声明PHP与浏览器交...

使用Sphinx对索引进行搜索

Sphinx对索引进行搜索主要分为以下几步:1、用户输入查询语句。 2、对查询语句进行词法分析,语法分析,及语言处理。 3、搜索索引,得到符合语法树的文档。 4、根据得到的文档和查询语句...

PHP的几个常用加密函数

MD5加密: string md5 ( string $str [, bool $raw_output = false ] ) 1.md5()默认情况下以 32 字符十六进制数字形式返回...

如何阻止网站被恶意反向代理访问(防网站镜像)

什么是反向代理? 先说说正向代理的概念: 正向代理,也就是传说中的代理,他的工作原理就像一个跳板。简单的说,我是一个用户,我访问不了某网站,但是我能访问一个代理服务器。这个代理服务器呢,...