简体   繁体   English

使用regexp的MySQL查询在Drupal中不起作用

[英]MySQL query with regexp not working in Drupal

I have the following query, courtesy of SO: 我有以下查询,由SO提供:

SELECT field_website_value FROM field_data_field_website  WHERE field_website_value NOT REGEXP('^(https?://|www\\.)[\.A-Za-z0-9\-]+\\.[a-zA-Z]{2,4}(/\S*)?') AND field_website_value!=''

When executing this query directly in the MySQL client, it works (shows the values that don't match the pattern). 直接在MySQL客户端中执行此查询时,它可以工作(显示与模式不匹配的值)。

However when putting it in Drupal, it stops working, it just returns the rows which are not empty. 但是,当将其放入Drupal时,它将停止工作,仅返回不为空的行。

$query = "SELECT field_website_value FROM field_data_field_website  WHERE field_website_value NOT REGEXP('^(https?://|www\\.)[\.A-Za-z0-9\-]+\\.[a-zA-Z]{2,4}(/\S*)?') AND field_website_value!=''";
$res = db_query($query)->fetchAll();
echo count($res);
echo "<pre>";print_r($res);die();

Is there any way I can use Regexp in Drupal? 有什么方法可以在Drupal中使用Regexp?

Note: getting all rows and applying the regex in PHP isn't an option. 注意:获取所有行并在PHP中应用正则表达式不是一种选择。

I'm no drupal expert but I bet db_query function is doing a mysql_real_escape_string() call which will mess up the regular expression, are there any other functions you can pass that won't do this? 我不是drupal专家,但我敢打赌db_query函数正在执行mysql_real_escape_string()调用,该调用会弄乱正则表达式,还有其他可以传递的函数将无法执行此操作吗?

Actually it is the {} brackets causing the issue, you need to pass the data as a variable, 实际上是引起问题的{}括号,您需要将数据作为变量传递,

$query = "SELECT field_website_value FROM field_data_field_website  WHERE field_website_value NOT REGEXP('%s') AND field_website_value!=''";
$regexp = '^(https?://|www\\.)[\.A-Za-z0-9\-]+\\.[a-zA-Z]{2,4}(/\S*)?';
db_query($query, $regexp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM