我目前正在为一家公司开发 MVC 样式框架,出于安全原因,我需要确保通过查询字符串传递的 Controller /方法是 RFC 的有效字符(我找不到) .
我需要能够根据 PHP 解释器的允许来验证/清理类名
例如:
class SomEFunk__YClAssName extends Controller
{
}
我需要某种正则表达式来验证 SomEFunk__YClAssName
并在需要时对其进行清理!这也是方法的原理。
有几点需要考虑
- 开头的数字
- 只允许下划线
- 允许某些 PHP 特殊字符。
关于此或可能的表达式的任何信息都会非常有帮助。
这是我的一些路由器代码,因此您可以看到我需要在哪里实现它:
private function prepareQueryString()
{
if(strlen($this->query_string) == 0)
{
return;
}
//Remove [ending|starting|multiple] slashes
$this->query_string = preg_replace('/^\/+|\/+$|\/(?=\/)/', '', $this->query_string);
foreach(explode('/',$this->query_string) as $Key => $Value)
{
if($Key == 0)
{
$Controller = $this->AssignController($Value);
}
if($Key == 1)
{
$this->AssignMethod($Value);
}else
{
$this->AssignParam($Value);
}
}
//Build RouterVar stdClass
}
public function AssignController(String $Controller)
{
if(!empty($Controller))
{
//Sanitize
}
}
public function AssignMethod(String $Method)
{
if(!empty($Method))
{
//Sanitize
}
}
public function AssignParam(String $Param)
{
$this->params[] = $Param;
}
您会在需要检查的地方看到注释“Sanitize”。
最佳答案
我相信您正在寻找短类名的正则表达式,例如SomeClass
是:
<?php
preg_match(
'/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$/',
$input
);
根据:http://php.net/manual/en/language.oop5.basic.php
对于命名空间类,例如App\SomeClass
是:
<?php
preg_match(
'/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*$/',
$input
);
关于php - 使用正则表达式验证类/方法名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3195614/