我希望在PHP中找到some words
包含在字符串
if (strpos($str,array("select","update","insert into","delete") !== False){
echo "string contain SQL statements";
}
任何人都知道怎么做?
答案 0 :(得分:2)
如果您想要检测字符串中是否包含任何单词,您可以这样做:
$sql_words = array(...);
if(count(array_intersect(explode(' ', strtolower($str)), $sql_words)) > 0) {
}
虽然字符串包含select
,但并不意味着它是SQL查询。但我想这取决于具体情况。
根据字符串,您可能还想使用str_word_count
。
答案 1 :(得分:0)
您可以使用strstr功能,如下所示:
$string_to_be_searched_in = "SELECT * FROM MyTable";
$sqlWords = array("insert", "update", "delete" ...);
foreach($sqlWords as $word) {
if(strstr($string_to_be_searched_in, $word) != FALSE) {
echo "The given string is an SQL statement";
}
}
答案 2 :(得分:0)
试
$string = "i m where ?";
$searchfor = array("select","from","where","and","and",update,"set","insert","into","value");
foreach($searchfor as $v) {
if(false !== strpos($string, $v) ) {
//Found a word
echo 'Found '.$v;
break;
}
}
参考:
也学习
答案 3 :(得分:0)
您要使用的功能是preg_match()。它使用regular expressions将模式与字符串匹配,并返回模式匹配的次数 - 如果您将数组作为参数传递,您还可以使用它从字符串中提取信息(在您的示例中,你可以拉出查询的内容)。
您应该阅读these pages使用PHP的正则表达式,this page将允许您测试正则表达式。