php - Help with preg_replace

admin2025-06-02  1

I'm trying to remove 6 lines from my RSS feed using preg_replace. I've tested the regex online and it seems to select the lines properly. Somehow the lines are not getting deleted

add_action('the_content_feed', 'remove_rss_links');
    function remove_rss_links($content) {
    return preg_replace(".*appeared first on.*$", "", $content);
}

Here is part of the feed I'm trying to tweak.

<category><![CDATA[General]]></category>

        <guid isPermaLink="false">/?p=38</guid>
                <description><![CDATA[<p>Below is the directive the committee gave to our architect (Ron Garl) relative to tee renovation: Number “6” tees Must be an option for all holes New “6” tee construction on holes: 1,4,6,8,9, and &#8230; <a class="more-link" href="/">Continue reading <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a rel="nofollow" href="/">Tees</a> appeared first on <a rel="nofollow" href="">Golf Course Renovation</a>.</p>
]]></description>
                                <content:encoded><![CDATA[<p>The post <a rel="nofollow" href="/">Tees</a> appeared first on <a rel="nofollow" href="">Golf Course Renovation</a>.</p>
]]></content:encoded>

Any help with my preg_replace would be appreciated.

I'm trying to remove 6 lines from my RSS feed using preg_replace. I've tested the regex online and it seems to select the lines properly. Somehow the lines are not getting deleted

add_action('the_content_feed', 'remove_rss_links');
    function remove_rss_links($content) {
    return preg_replace(".*appeared first on.*$", "", $content);
}

Here is part of the feed I'm trying to tweak.

<category><![CDATA[General]]></category>

        <guid isPermaLink="false">http://heronsglengolf/?p=38</guid>
                <description><![CDATA[<p>Below is the directive the committee gave to our architect (Ron Garl) relative to tee renovation: Number “6” tees Must be an option for all holes New “6” tee construction on holes: 1,4,6,8,9, and &#8230; <a class="more-link" href="http://heronsglengolf/tees/">Continue reading <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a rel="nofollow" href="http://foobar/tees/">Tees</a> appeared first on <a rel="nofollow" href="http://foobar">Golf Course Renovation</a>.</p>
]]></description>
                                <content:encoded><![CDATA[<p>The post <a rel="nofollow" href="http://foobar/tees/">Tees</a> appeared first on <a rel="nofollow" href="http://foobar">Golf Course Renovation</a>.</p>
]]></content:encoded>

Any help with my preg_replace would be appreciated.

Share Improve this question asked Mar 11, 2019 at 20:48 user302477user302477 133 bronze badges 3
  • Have you tried using an XML parser instead? Parsing HTML/XML via regular expressions only works for super, super simple markup, and isn't a simple task. Also, can you provide some context, what problem does removing these tags solve? – Tom J Nowell Commented Mar 11, 2019 at 21:17
  • I'm open to any ideas to remove these lines from my RSS feed. The lines I'm trying to remove contain the <p> tag, which of course are throughout the file. I only want to remove 2 per post.For each post I get a line tht reads "The Post xxx appeared first on xxx" at the bottom of both the excerpt and full text. I'm using an email service to send out the posts and the line (and the links contained in it) are distracting and unnecessary. – user302477 Commented Mar 11, 2019 at 23:26
  • That's fine, just change your question to be about removing the lines and use preg_replace as an example of what you've tried. Otherwise a lot of people will see that, think "eek regex" and avoid it. Now that I know it's the post appeared first on xxx that you're trying to remove, that's actually a much easier question that probably doesn't need the XML filtering at all – Tom J Nowell Commented Mar 12, 2019 at 14:03
Add a comment  | 

1 Answer 1

Reset to default 0

As you said in your comment:

I only want to remove 2 per post.For each post I get a line tht reads "The Post xxx appeared first on xxx" at the bottom of both the excerpt and full text.

I'm glad you said so, regular expressions and XML parsing aren't the nicest to deal with, but it turns out you don't need to use either. This isn't a WP feature but a Yoast SEO feature, and it can be turned off in the options:

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748815758a313954.html

最新回复(0)