如何在包含MySQL中特定字符的字符串中添加字符串

时间:2013-07-03 11:37:10

标签: mysql replace

我有一个wordpress网站,需要在每个包含特定字词的特定链接(例如此链接)之后添加string rel="nofollow"。我有这个字符串:

a href="http://www.this-link-is.com/ANYTHING"

并且在包含此链接的每个字符串后需要add rel="nofollow"
      a href="http://www.this-link-is.com/ANYTHING" rel="nofollow"

所以它应该像

update wp_posts set post_content = replace(post_content, '%this-link%>', '%this-link% rel="nofollow">');

我怎么能这样做?谢谢。

1 个答案:

答案 0 :(得分:1)

您正在寻找的是REGEXP_REPLACE功能。您需要将其定义为UDF,例如:

https://launchpad.net/mysql-udf-regexp

使用该链接中的REGEXP_REPLACE UDF,您的查询将如下所示:

UPDATE `wp_posts`
SET `wp_content` = REGEXP_REPLACE(`wp_content`, '(this-link[^"]+")', '\1 rel="nofollow')
WHERE `link` LIKE '%this-link%';
相关问题