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脚本守护进程原理与实现方法详解

本文实例讲述了php脚本守护进程原理与实现方法。分享给大家供大家参考,具体如下: 思路: 1. while 循环,若当前没有数据要操作可以休眠; 2. crontab 脚本每隔固定时间段...

Http 1.1 Etag 与 Last-Modified提高php效率

Http 1.1 Etag 与 Last-Modified提高php效率

在 Blog 盛行的今天,一些 Web 应用需要解析大量的 RSS Feed .如何提高效率是个非常重要的问题.在 MagpieRSS 的 Features 中列举了这样的一条: HT...

php代码审计比较有意思的例子

php代码审计比较有意思的例子

代码审计比较有意思的例子貌似是去年 ecshop支付漏洞偶然出来的一个例子,感觉不错。分享下 复制代码 代码如下:<?php$a=addslashes($_GET['a']);$b...

PHP中strcmp()和strcasecmp()函数字符串比较用法分析

本文实例讲述了PHP中strcmp()和strcasecmp()函数字符串比较用法。分享给大家供大家参考,具体如下: 一、PHP中strcmp()函数用于比较两个字符串(区分大小写),其...

一个php短网址的生成代码(仿微博短网址)

分享一个php短网址的生成代码。 复制代码 代码如下: <!DOCTYPE html> <html lang="en"> <head> <met...