PHP-如何验证正则表达式本身? [英] php - how to validate a regular expression itself?

查看:68
本文介绍了PHP-如何验证正则表达式本身?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要验证用户输入,该用户输入可能是用于过滤自己应用程序中字段的正则表达式,如何检查该正则表达式的有效性?

I need to validate user input that might be a regular expression for filtering fields in his/her own application, how can I check the validity of that regex?

推荐答案

将其传递给对preg_match()的伪调用,看看它是否返回false(指示错误)?

Pass it into a dummy call to preg_match(), and see if it returns false (indicating an error)?

注意::根据您打算在哪里运行此应用程序,我会对允许用户执行此操作的决定提出质疑.您可能正在为拒绝服务攻击做好准备;恶意用户需要做的就是提供一个令人难以置信的长而复杂的regex字符串,您的服务器将停止运行.

Note:: Depending on where you intend this application to be run, I would question the decision to allow users to do this. You could be setting yourself up for a denial-of-service attack; all a malicious user needs to do is provide an incredibly long, complicated regex string, and your server will grind to a halt.

这篇关于PHP-如何验证正则表达式本身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆