MySQL:将具有 StackOverflow 样式编码 URL 的字段转换为 HTML
MySQL: Convert Field with StackOverflow-style Coded URLs to HTML
我们有一个遗留数据库 table 充满了类似 Whosebug 的编码 URLs。
=====================================================================================
| TABLE_COMMENTS |
=====================================================================================
| f_ID | f_comment |
-------------------------------------------------------------------------------------
| 1 | To buy coffee [click here](https://google.com) or [here](https://bing.com) |
| 2 | Check [this out](https://whosebug.com) |
| 3 | [Cat Photos](https://google.com/images/?cats) |
=====================================================================================
我需要 SELECT 这些作为标准 HTML 所以它们看起来像下面 table.
| f_comment |
----------------------------------------------------------------------------------------------------
| To buy coffee <a href="https://google.com">click here</a> or <a href="https://bing.com">here</a> |
| Check <a href="https://whosebug.com">this out</a> |
| <a href="https://google.com/images/?cats">Cat Photos</a> |
====================================================================================================
如果 f_comment
字段中只有一个 URL,我有一种方法可以使用,但我不知道如何让它工作,比如 row 1
其中有两个 URLs.
SELECT
CONCAT(
substring_index(substring_index(f_comment, '[', 1),')', 1),
"<a href=",
substring_index(substring_index(f_comment, '(', -1),')', 1),
">",
substring_index(substring_index(f_comment, '[', -1),']', 1),
"</a>"
)
AS f_replacementtext
FROM TABLE_COMMENTS;
我的 不完整 查询的结果,它错过了 f_ID
1
的第一个 URL
==========================================================
| f_comment |
----------------------------------------------------------
| To buy coffee <a href="https://bing.com">here</a> |
| Check <a href="https://whosebug.com">this out</a> |
| <a href="https://google.com/images/?cats">Cat Photos</a> |
==========================================================
set @col =
'To buy coffee <a src=https://google.com>click here</a> or <a src=https://bing.com>here</a>';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT REGEXP_REPLACE( @col, '<a src=(http.*?)>(.*?)</a>', '[]()');
+----------------------------------------------------------------------------+
| REGEXP_REPLACE( @col, '<a src=(http.*?)>(.*?)</a>', '[]()') |
+----------------------------------------------------------------------------+
| To buy coffee [click here](https://google.com) or [here](https://bing.com) |
+----------------------------------------------------------------------------+
走另一条路...
SET @col =
'To buy coffee [click here](https://google.com) or [here](https://bing.com)';
SELECT
REGEXP_REPLACE( @col, '\[(.*?)\][(](http.*?)[)]',
'<a src=$2>$1</a>' );
+------------------------------------------------------------------------------------------------------+
| REGEXP_REPLACE( @col, '\[(.*?)\][(](http.*?)[)]',
'<a src=$2>$1</a>' ) |
+------------------------------------------------------------------------------------------------------+
| To buy coffee <a src=https://google.com>click here</a> or <a src=https://bing.com>here</a> |
+------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
我们有一个遗留数据库 table 充满了类似 Whosebug 的编码 URLs。
=====================================================================================
| TABLE_COMMENTS |
=====================================================================================
| f_ID | f_comment |
-------------------------------------------------------------------------------------
| 1 | To buy coffee [click here](https://google.com) or [here](https://bing.com) |
| 2 | Check [this out](https://whosebug.com) |
| 3 | [Cat Photos](https://google.com/images/?cats) |
=====================================================================================
我需要 SELECT 这些作为标准 HTML 所以它们看起来像下面 table.
| f_comment |
----------------------------------------------------------------------------------------------------
| To buy coffee <a href="https://google.com">click here</a> or <a href="https://bing.com">here</a> |
| Check <a href="https://whosebug.com">this out</a> |
| <a href="https://google.com/images/?cats">Cat Photos</a> |
====================================================================================================
如果 f_comment
字段中只有一个 URL,我有一种方法可以使用,但我不知道如何让它工作,比如 row 1
其中有两个 URLs.
SELECT
CONCAT(
substring_index(substring_index(f_comment, '[', 1),')', 1),
"<a href=",
substring_index(substring_index(f_comment, '(', -1),')', 1),
">",
substring_index(substring_index(f_comment, '[', -1),']', 1),
"</a>"
)
AS f_replacementtext
FROM TABLE_COMMENTS;
我的 不完整 查询的结果,它错过了 f_ID
1
==========================================================
| f_comment |
----------------------------------------------------------
| To buy coffee <a href="https://bing.com">here</a> |
| Check <a href="https://whosebug.com">this out</a> |
| <a href="https://google.com/images/?cats">Cat Photos</a> |
==========================================================
set @col =
'To buy coffee <a src=https://google.com>click here</a> or <a src=https://bing.com>here</a>';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT REGEXP_REPLACE( @col, '<a src=(http.*?)>(.*?)</a>', '[]()');
+----------------------------------------------------------------------------+
| REGEXP_REPLACE( @col, '<a src=(http.*?)>(.*?)</a>', '[]()') |
+----------------------------------------------------------------------------+
| To buy coffee [click here](https://google.com) or [here](https://bing.com) |
+----------------------------------------------------------------------------+
走另一条路...
SET @col =
'To buy coffee [click here](https://google.com) or [here](https://bing.com)';
SELECT
REGEXP_REPLACE( @col, '\[(.*?)\][(](http.*?)[)]',
'<a src=$2>$1</a>' );
+------------------------------------------------------------------------------------------------------+
| REGEXP_REPLACE( @col, '\[(.*?)\][(](http.*?)[)]',
'<a src=$2>$1</a>' ) |
+------------------------------------------------------------------------------------------------------+
| To buy coffee <a src=https://google.com>click here</a> or <a src=https://bing.com>here</a> |
+------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)