php - 如何根据正则表达式选择行?

标签 php mysql regex

我有一个这样的表:

// mytable
+----+----------------------+
| id |       sentence       |
+----+----------------------+
| 1  | thIs! is, a** tEst?  |
| 2  | and also This ^ is&  |
| 3  | --it^% is tOo.       |
+----+----------------------+

我还有一个变量:

$str = "this-is-a-test";
/* the above string is made according to this:
   strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', $str)));
*/

也是我的查询:

SELECT * FROM mytable WHERE sentence = '$str';

现在我想要这个输出:

+----+----------------------+
| id |       sentence       |
+----+----------------------+
| 1  | thIs! is, a** tEst?  |
+----+----------------------+

如何在搜索前临时在列上应用该正则表达式?

最佳答案

您可以在 MySQL 中使用正则表达式执行搜索:)
不要混淆!RegEx 将由 SGBD 执行,而不是由 PHP 执行!!

SELECT * FROM mytable WHERE sentence REGEXP '$str';

有关更多信息,请访问 MySQL 文档 MySQL REGEXP

关于php - 如何根据正则表达式选择行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35412574/

相关文章:

mysql - R - 使用 ""将日期/时间变量推送到 SQL 查询中

php - 连接错误 (1044) 用户访问被拒绝

php - 使用正则表达式和 ffmpeg 获取视频的尺寸

正则表达式匹配引号内的模式

php - 在mysql中更新用户信息的问题

php - 如何在php中格式化日期2015-12-25T10 :11:14. 000Z以存储到mysql中的日期时间?

php - Mysql连接两个表

php - 使用ajax和jquery将数据插入数据库

php - 如果 php 为 null 或为空,则更新日期以显示

regex - 有没有一种有效的方法可以从 Postgres 中的列值中抓取子字符串?