【问题标题】:Removing links from posts in wordpress using query使用查询从wordpress中的帖子中删除链接
【发布时间】:2013-10-21 19:43:03
【问题描述】:

我对mysql不太擅长,所以我需要一些帮助。

我正在尝试从我的 wordpress 博客中删除一些特定链接。我有很多指向多个域的传出链接,我只想删除指向其中一个域的链接。例如所有指向 dontneedlink.com 或以 dontneedlink.com/(某些子页面)开头的链接

我已经尝试过了

UPDATE wp_posts SET post_content = REPLACE (
post_content,
'Item to replace here',
Replacement text here');

但这对我来说不是上帝,因为我有很多关键词,还有很多组合。

所以我需要某种查询来识别包含 dontneedlink.com 的链接并删除 href 文本并保持关键字不变。

<  a href=”http:// dontneedlink.com”>Test</a> -> Test 

<  a href=”http:// dontneedlink.com”>Test Again</a> -> Test Again

<  a href=”http:// dontneedlink.com/childpage”>Test Again 2</a> -> Test Again 2

这可能吗? 这样可以节省我很多时间,而不是从一个帖子转到另一个帖子并手动删除。

【问题讨论】:

    标签: mysql hyperlink


    【解决方案1】:

    您需要使用“喜欢”来查找帖子和子字符串索引以删除不需要的内容

    substring_index( substring_index(table.column, 'href=”', -1), '"',1) 将提取您的链接

    mysql> SELECT REPLACE(table_column, substring_index( substring_index(table.column, 'href=”', -1),  '"',1) , '');
    

    这将为您提供没有链接的文本。你会留下

    <  a href=””>Test</a>
    

    然后执行另一个替换或子字符串索引以删除任何不需要的残留物。

    最后用正确的过滤器在哪里运行它:

    UPDATE wp_posts SET post_content = REPLACE (
    post_content,
    'Item to replace here',
    Replacement text here')
    where mycolumn like "%dontneedthisdomain.com%"
    

    【讨论】:

      【解决方案2】:

      在@AdrianBR 上扩展和简化 运行以下 SQL...

      UPDATE table_name SET column_name = REPLACE(column_name, substring_index( substring_index(column_name, 'href="', -1),  '"', 1),'');
      UPDATE table_name SET column_name = REPLACE(column_name, '<a href="">','');
      UPDATE table_name SET column_name = REPLACE(column_name, '<a href="" target="_blank">','');
      UPDATE table_name SET column_name = REPLACE(column_name, '</a>','');
      

      class 和其他属性被添加到链接时,生活变得很棘手,但上述方法应该可以调整以涵盖这些情况(取决于您的要求)

      【讨论】:

        猜你喜欢
        • 2013-09-21
        • 1970-01-01
        • 2014-03-05
        • 1970-01-01
        • 2018-10-23
        • 1970-01-01
        • 2012-07-02
        • 2012-07-01
        • 1970-01-01
        相关资源
        最近更新 更多