从PHP字符串中删除HTML - Wordpress RSS Feed模板

Thanks for all the help; nothing working as of yet, so adding more detail.

I'm using the below function in functions.php to allow me to use my own RSS template, which is working fine.

add_action('init', 'customRSS');
function customRSS(){
        add_feed('shows', 'customRSSFunc');
}
function customRSSFunc(){
        get_template_part('rss', 'shows');
}

The php template for the feed is:

<?php
header('Content-Type: ' . feed_content_type('rss2') . '; charset=' . get_option('blog_charset'), true);
$more = 1;
echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
do_action( 'rss_tag_pre', 'rss2' );
?>
<rss version="2.0"
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
    xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
    <?php do_action( 'rss2_ns' );?>>

<channel>
    <title><?php wp_title_rss(); ?></title>
    <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
    <link><?php bloginfo_rss('url') ?></link>
    <description><?php bloginfo_rss("description") ?></description>
    <lastBuildDate><?php
        $date = get_lastpostmodified( 'GMT' );
        echo $date ? mysql2date( 'r', $date, false ) : date( 'r' );
    ?></lastBuildDate>
    <language><?php bloginfo_rss( 'language' ); ?></language>
    <sy:updatePeriod><?php
        $duration = 'hourly';
        echo apply_filters( 'rss_update_period', $duration );
    ?></sy:updatePeriod>
    <sy:updateFrequency><?php
        $frequency = '1';
        echo apply_filters( 'rss_update_frequency', $frequency );
    ?></sy:updateFrequency>
    <?php do_action( 'rss2_head'); while( have_posts()) : the_post();?>
    <item>
        <title><?php the_title_rss(); ?></title>
        <date><?php echo customevent_get_start_date( null, false, 'd-m-Y' ); ?></date>
        <time><?php echo customevent_get_start_date( null, false, 'H:i' ); ?></time>
        <price><?php echo customevent_get_cost(); ?></price>
        <link><?php the_permalink_rss(); ?></link>
        <imageurl><?php the_post_thumbnail_url(); ?></imageurl>
        <description>
            <?php
            $content = the_content();
            echo $content ; ?>
        </description>
        <?php rss_enclosure(); ?>
        <?php do_action('rss2_item'); ?>
    </item>
<?php endwhile; ?>
</channel>
</rss>

The part I'm having issues with is the description (the_content). The output on the feed is showing p tags and more, as well as   in numerous locations. Both of which need to be stripped.

I've tried:

    <description>
        <?php
        $content = the_content();
        echo wp_filter_nohtml_kses($content); ?>
    </description>

and

    <description>
        <?php
        $content = the_content();
        echo strip_tags(html_entity_decode($content)); ?>
    </description>

Output in feed:

<description>
<p><strong>It took Michael Portillo little more than 10 years to get a seat in the Commons and then rise in power and esteem to a point where he was a favoured leader of his party and possible future PM. A track record like that suggests a privileged friend of the rich and famous ,but since leaving the house almost a decade ago Michael has endeared himself to many with his obvious respect for solid workmanship as found in our great Victorian Railways and the daily life of ordinary hard working citizens. Listen to his story, told with a “ parliamentary standup ” wit, and then feel free to question him about it.      </strong></p>
<p><strong>Full Price &#8211; £19.50 </strong></p>
<p><strong>Concession &#8211; £18.50                                                                                                                                                   </strong></p>
        </description>

You can wrap str_replace with strip_tags like this:

<?php
function the_content(){
    echo '<p>&nbspContent</p>';
}

$content = the_content();
echo str_replace(' ','',strip_tags($content));
?>

I tested this and it works fine. If this isn't working for you could you please show the content of your function?

EDIT:

I would also recommend you look at html_entity_decode here:

https://www.w3schools.com/php/func_string_html_entity_decode.asp

This is the best way to achieve what you want. My answer above is to provide a direct answer to what you ask but this is the better alternative.

use this one.

wp_filter_nohtml_kses( string $data );

check this link. https://developer.wordpress.org/reference/functions/wp_filter_nohtml_kses/