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以ROOT权限执行系统命令的方法

用来作为解决php以root权限执行一些普通用户不能执行的命令或应用的参考。 其实php里的popen()函数是可以解决这个问题的,但是由于某些版本的linux(如我使用的Centos...

PHP编程实现的TCP服务端和客户端功能示例

本文实例讲述了PHP编程实现的TCP服务端和客户端功能。分享给大家供大家参考,具体如下: 1、修改php.ini,打开extension=php_sockets.dll 2、服务端程序S...

让PHP开发者事半功倍的十大技巧小结

如果你使用一面大镜子作为冲浪板会发生什么?或许你会在较短的时间内征服海浪,但是你肯定从内心深处明白,这不是冲浪的正确选择。同样的道理也适用于PHP编程,尽管这样的类比听起来有一些古怪。我...

PHP生成UTF8文件的方法

复制代码 代码如下:<?php $f=fopen("test.txt", "wb"); $text=utf8_encode("a!"); //先用函数utf8_encode将所需写...

php获取手机端的号码以及ip地址实例代码

我们在用PHP写移动端程序的时候,有的时候需要直接获取手机号码以及对应的IP地址内容,在此我们给大家整理了详细完整的代码内容,需要的朋友们测试下。 <?php /**...