7个鲜为人知却非常实用的PHP函数
作者:junjie 时间:2023-10-15 03:46:47
概述
PHP有着众多的内置函数,其中大多数函数都被开发者广发使用。但也有一些同样有用却被遗忘在角落,本文将介绍7个鲜为人知功能却非常酷的函数。
highlight_string()
当需要在网页中展示PHP代码时,highlight_string()函数就显得非常有用。该函数通过PHP内置定义的颜色,返回函数中代码的高亮显示版本。
<?php
highlight_string('<?php echo "hello world" ; ?>');
echo highlight_string('<?php echo "hello world" ; ?>',true);
?>
str_word_count()
这个函数可以方便的将输入的字符串参数中的单词个数返回。
<?php
$str = "hello world";
echo str_word_count($str); //输出 2
?>
levenshtein()
该函数可以方便的返回两个参数之间的levenshtein(编辑距离)。曾经遇到过一个需求,用户在编辑身份证的时候,限制用户只能修改4位数字,使用的就是这个函数。
<?php
$idcard='230406198506206797';
$newIdcard='230406198506207798';
echo levenshtein($idcard,$newIdcard); //输出 2
?>
get_defined_vars()
这个函数在调试程序的时候非常有用,它会返回包含所有已定义变量的数组,其中包含环境、系统以及用户自定义变量。
<?php
var_dump(get_defined_vars());
?>
escapeshellcmd()
该函数用来跳过字符串中的特殊符号,防止恶意用户耍花招破解服务器系统。可以搭配exec()与system()函数使用。
<?php
$command = './configure '.$_POST['configure_options'];
$escaped_command = escapeshellcmd($command);
system($escaped_command);
?>
checkdate()
该函数可以用来检测日期参数的有效性。它可以验证输入的每一个参数的合法性。
<?php
var_dump(checkdate(12, 31, 2000));
var_dump(checkdate(2, 29, 2001));
//输出
//bool(true)
//bool(false)
?>
php_strip_whitespace()
该函数会返回删除了注释与空格后的PHP源码。这对实际代码数量和注释数量的对比很有用。
<?php
// 注释1
/*
* 注释2
*/
echo php_strip_whitespace(__FILE__);
do_nothing();
?>
标签:鲜为人知,PHP函数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
为非IE浏览器添加mouseenter,mouseleave事件的实现代码
2024-02-23 19:26:11
如何基于Python实现自动扫雷
2023-12-13 15:51:08
![](https://img.aspxhome.com/file/2023/8/85128_0s.jpg)
JavaScript实现获取select下拉框中第一个值的方法
2024-04-22 12:50:05
![](https://img.aspxhome.com/file/2023/7/136087_0s.png)
20行python代码的入门级小游戏的详解
2023-07-15 01:25:31
Go语言基础学习教程
2024-02-06 07:12:11
![](https://img.aspxhome.com/file/2023/4/112244_0s.png)
使用Python开发windows GUI程序入门实例
2023-01-18 16:54:45
GO语言运行环境下载、安装、配置图文教程
2024-05-11 09:09:11
![](https://img.aspxhome.com/file/2023/7/127027_0s.png)
python之数字图像处理方式
2023-02-02 18:27:09
![](https://img.aspxhome.com/file/2023/4/78584_0s.jpg)
vscode 配置eslint和prettier正确方法
2022-12-31 04:54:44
![](https://img.aspxhome.com/file/2023/4/114704_0s.png)
Go语言中的内存布局详解
2024-05-21 10:19:55
CMS不要让MySQL为你流泪
2008-12-11 14:38:00
Python Web框架Flask中使用七牛云存储实例
2022-11-25 17:20:27
Python 写入训练日志文件并控制台输出解析
2021-10-11 01:27:10
![](https://img.aspxhome.com/file/2023/5/131555_0s.jpg)
sqlserver 数据库压缩与数据库日志(ldf)压缩方法分享
2024-01-17 12:33:46
javascript让浏览器实现复读机的功能
2008-10-10 11:49:00
在Python的Flask框架中实现全文搜索功能
2022-12-20 14:38:18
MySQL中Binary Log二进制日志文件的基本操作命令小结
2024-01-12 22:24:20
![](https://img.aspxhome.com/file/2023/9/79999_0s.png)
定义列表: DL DT DD
2009-05-06 13:08:00
windows10下mysql 8.0 下载与安装配置图文教程
2024-01-17 19:45:51
![](https://img.aspxhome.com/file/2023/2/110302_0s.png)
使用python检测手机QQ在线状态的脚本代码
2023-03-27 02:12:06