Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正则表达式中的php变量?_Php_Sql_Mysql_Regex_Variables - Fatal编程技术网

正则表达式中的php变量?

正则表达式中的php变量?,php,sql,mysql,regex,variables,Php,Sql,Mysql,Regex,Variables,我有一个变量$word,我想在select语句中匹配它 类似这样的内容:“从表中选择*列regex'[0-9]$word'” 但是这不起作用,所以我怎样才能把一个变量放到正则表达式中呢 谢谢您可以尝试使用preg_quote()来转义字符串: $sql = "select * from table where column regexp '[0-9]" . preg_quote($word) . "'"; 这会出现一些问题,因为MySQL(假设这是您的数据库)对哪些字符比PCRE特殊可能有完全

我有一个变量$word,我想在select语句中匹配它 类似这样的内容:“从表中选择*列regex'[0-9]$word'” 但是这不起作用,所以我怎样才能把一个变量放到正则表达式中呢


谢谢

您可以尝试使用
preg_quote()
来转义字符串:

$sql = "select * from table where column regexp '[0-9]" . preg_quote($word) . "'";

这会出现一些问题,因为MySQL(假设这是您的数据库)对哪些字符比PCRE特殊可能有完全不同的理解。

您必须在SQL查询中正确地放置它,例如:

mysql_query("SELECT * FROM table WHERE column REGEXP 'prefix_".$word."'");

但是您需要记住,变量数据需要正确转义,我认为addslashes()就足够了。

如果您也能对我的答案投票,我将非常感谢;]