PHP判断字符内容是否含有代码
发布:HelloJq 时间:2025-11-01
PHP基础判断字符内容是否含有代码,基本够用,正则含有代码
function containsCodeBasic($content) { //判断内容是否含有代码
$codePatterns = [
// PHP 标签
'/<?php/i',
'/<?=/i',
'/<?/i',
'/?>/i',
// HTML 标签
'/<script[^>]*>/i',
'/</script>/i',
'/<iframe[^>]*>/i',
'/<form[^>]*>/i',
'/<input[^>]*>/i',
// 常见的函数调用
'/(eval|exec|system|shell_exec|passthru)s*(/i',
'/$_?(GET|POST|REQUEST|COOKIE|SERVER)/i',
// SQL 注入特征
'/(SELECT|INSERT|UPDATE|DELETE|DROP|UNION|WHERE)/i',
// JavaScript 事件
'/on(click|load|submit|mouseover|error)=/i',
// 文件包含
'/(include|require)(_once)?s*(/i',
];
foreach ($codePatterns as $pattern) {
if (preg_match($pattern, $content)) {
return true;
}
}
return false;
}