MySQL 是否有与 PHP 的 preg_replace 相当的工具?
我有一个匹配 MySQL 中的字段,我认为我可以使用正则表达式,但 MySQL 似乎没有完成这项工作所需的功能。场景如下:
我在 PHP 中有一个名为 $url 的变量。假设此变量设置为字符串“/article/my-article/page/2”。我在 MySQL 中还有一个 URL 表,我想从中提取内容。然而,存储在我的表中的 URL 包含通配符。
以前,我进行了这样的设置,以便存储在表中的值如下所示:“/article/%/page/%”。
通过该配置,我可以运行:
SELECT * FROM urls WHERE '$url' LIKE url
这将匹配,这是所需的功能。
我现在想做的是允许使用更高级的通配符,这样我的 MySQL 数据就可以是“/article/{{slug}}/page/{{”,而不是“/article/%/page/%”页号}}”。
我想使用相同的 $url 输入创建一个与此数据匹配的 SQL 查询。 LIKE 不再是正确的比较,因为我没有使用内置的“%”通配符,而是使用 {{.*}}。有什么想法如何实现这一点?
I have a to match a field in MySQL, for which I thought I could use a regular expression, but it appears that MySQL doesn't have the functionality I need to do the job. Here's the scenario:
I have a variable in PHP called $url. Let's say this variable is set as the string "/article/my-article/page/2". I also have a table of URLs in MySQL from which I would like to pull content. The URLs stored in my table, however, include wildcards.
Previously, I had this set up so that the value stored in the table looked like this: "/article/%/page/%".
With that configuration, I could just run:
SELECT * FROM urls WHERE '$url' LIKE url
And this would match, which is the desired functionality.
What I'd like to do now, is allow a more advanced wildcard, such that instead of "/article/%/page/%", my MySQL data could be "/article/{{slug}}/page/{{page_no}}".
I want to create a SQL query that will match this data, using the same $url input. LIKE is no longer the correct comparison, since I'm not using the built-in "%" wildcard, but rather {{.*}}. Any ideas how to accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
MySQL 中有一个用户定义函数库可以为您提供 preg_replace:
http://www.mysqludf.org/lib_mysqludf_preg/
There is a library of user defined functions that gives you preg_replace in MySQL:
http://www.mysqludf.org/lib_mysqludf_preg/
听起来您想要做的是在数据库中使用新语法,其中 URL 具有占位符,您将传递给基于 php (sprintf) 的变量替换代码,但仍然能够进行原始比较以匹配 URL。
如果我理解正确的话,您想要采用一种新的 URL 格式
并将其与类似的内容进行匹配。
提到的 preg 插件 sagi 可以进行您需要的替换,这会将您新格式化的 URL 之一转换为您用于确定匹配的原始格式LIKE 语法。以下查询:
将新的网址(/article/{{slug}}/page/{{page_no}})转换为原来的网址
,然后可以通过原始查询反馈,如下所示:
一些二进制发行版像 MAMP、XAMMP 等已经安装了该插件,但它没有安装在很多系统上,例如 Macports / Ubuntu。这里有几篇关于安装 preg 插件的文章。希望有帮助。
http:// Quickshiftin.com/blog/2011/04/installing-mysql-preg-plugin-osx-macports/
http://quickshiftin.com/blog/2011/12/installing-the-mysql-preg-plugin-on-ubuntu-使用-apt-get/
It sounds like what you want to do is have the new syntax in the database where the URLs have placeholders you would pass to your php-based (sprintf) variable replacement code, but still be able to do the original comparisons to match the URL.
If I understand correctly you want to take a new URL format
and match it against something like
The preg plugin sagi mentioned can do the substitution you need, which will turn one of your newly formatted URLs into the original format you used to determine the match using the LIKE syntax. The following query:
Would turn the new url (/article/{{slug}}/page/{{page_no}}) into what it was originally
which can then be fed back through your original query, something like this:
Some binary distributions like MAMP, XAMMP etc have the plugin already installed, but it isn't installed on a lot of systems like Macports / Ubuntu. Here are a couple of articles about installing the preg plugin. Hope it helps.
http://quickshiftin.com/blog/2011/04/installing-mysql-preg-plugin-osx-macports/
http://quickshiftin.com/blog/2011/12/installing-the-mysql-preg-plugin-on-ubuntu-with-apt-get/
上面的用户sagi提到http://www.mysqludf.org/lib_mysqludf_preg/ 但由于这个答案和大多数教程一样非常古老,为了这个问题的新手,我想对此进行扩展。
首先,这个库真的很棒,从经验来看,我可以说它似乎一直在维护,并且在 2015 年仍然完美运行。
为了安装它并运行,我只能找到一些非常过时的教程,所以我想分享我的内容这对我在 Ubuntu 14.04 上安装最新稳定版本(v1.1)有用吗:
您现在应该可以使用以下所有功能:
The user sagi above mentions http://www.mysqludf.org/lib_mysqludf_preg/ but as this answer is very old as are most of the tutorials, I wanted to expand on this for the sake of newcomers to this question.
Firstly, the library is really great and speaking from experience I can say it seems to have been maintained and is still working flawlessly in 2015.
To get it installed and working, I could only find some very dated tutorials so thought I would share what I did that worked for me installing latest stable release (v1.1) on Ubuntu 14.04:
You should now have all the following functions available to you:
从 mysql 5.5 开始,您可以使用 RLIKE :
以 REGEXP 样式存储 url,并使用
进行查询
SELECT * FROM urls WHERE '$url' RLIKE url;
或者保持LIKE风格,然后进行替换(% by .* , _ by .):
SELECT * FROM url WHERE '$url' RLIKE REPLACE(REPLACE(url, '%', '.*'), '_', '.');
为了完整起见,您必须进行其他替换来转义具有正则表达式意义的字符:? \ () [] ... (参见 php 函数 preg_quote)
Starting from mysql 5.5, you can use RLIKE :
either store url in REGEXP-style, and query with
SELECT * FROM urls WHERE '$url' RLIKE url;
or keep it in LIKE-style, and make a replacement (% by .* , _ by .):
SELECT * FROM urls WHERE '$url' RLIKE REPLACE(REPLACE(url, '%', '.*'), '_', '.');
To be complete, you would have to do other replacements to escape chars that are regexp-significant : ? \ () [] ... (see php function preg_quote)
我找到了解决方案。它并不理想,但我会将其放在这里,以防纯 SQL 解决方案永远不会出现。我可以将 MySQL 中的 url 字段保留为“/articles/%/page/%”,并添加另一个名为“变量”的字段,用于存储要使用的变量名称列表。这样我就可以使用原始查询,然后在检索数据后将返回值中的“%”字符替换为 PHP 中的“{{variable}}”和 sprintf。
不过,如果可能的话,我希望看到这个问题在 SQL 中得到解决,因为我认为这个概念很有价值。
I've found a solution. It's not ideal, but I'll put it in here in case a pure SQL solution never surfaces. I can leave the url field in MySQL equal to "/articles/%/page/%" and add another field called variables that stores a list of variable names to be used. That way I can use my original query, then replace the "%" characters in the return value with "{{variable}}" in PHP with sprintf after I've retrieved the data.
Still, I'd like to see this solved in SQL if possible, since I think the concept is valuable.