我有一个这样的表:
// 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/