How to remove redundant <p> and <br/> from the result of WordPress shortcode

I use shortcode to output the following html in a variable, but there are too many redundant code such as br and p tags, how to remove them? Thanks!

My shortcode function:

add_shortcode('portfolios', 'van_portfolios_shortcode');
function van_portfolios_shortcode( $atts, $content) {
    extract(shortcode_atts(array(
        'number'=>'9',
        'slug'=>''
    ), $atts)); 
   $str=van_portfolios($slug,$number,false);
   return $str;
}

function van_process_shortcode($content) {
    global $shortcode_tags;
    // Backup current registered shortcodes and clear them all out
    $orig_shortcode_tags = $shortcode_tags;
    $shortcode_tags = array();
    add_shortcode('portfolios', 'van_portfolios_shortcode');

    // Do the shortcode (only the one above is registered)
    $content = do_shortcode($content);
    // Put the original shortcodes back
    $shortcode_tags = $orig_shortcode_tags;
    return $content;
}
add_filter('the_content', 'van_process_shortcode', 7);

Correct makeup is

<div class="portfolio-item">
           <a class="overlay" href="#">
             <h3>...</h3>
             <p>...</p>
           </a>
           <div class="tools"><a href="#" class="zoomin" rel="lightbox">ZoomIn</a><a href="#" class="info">Info</a></div>
           <a href="#" class="item">...</a>
         </div>

Output:

<div class="portfolio-item">
           <a class="overlay" href="#">
             <br /><!--This <br />is redundant code-->
             <h3>...</h3>
             <p>...</p><p><!--This <p> is redundant code-->
           </a>
           <div class="tools"><a href="#" class="zoomin" rel="lightbox">ZoomIn</a><a href="#" class="info">Info</a></div>
           <p><!--This <p> is redundant code--><a href="#" class="item">...</a>
         </div>

Answers


Try adding this code to before the content is displayed:

UPDATED

// From your question 
$content = do_shortcode($content);
// Put the original shortcodes back
$shortcode_tags = $orig_shortcode_tags;

// Add this code 
$content = preg_replace( '%<p>&nbsp;\s*</p>%', '', $content ); // Remove all instances of "<p>&nbsp;</p>" to avoid extra lines.
$Old     = array( '<br />', '<br>' );
$New     = array( '','' );
$content = str_replace( $Old, $New, $content );

// From your question
return $content;

Need Your Help

AJAX database update

javascript ajax database

This is my first ajax attempt. I want to pass id variable to ajax function and update my database. When I run this code everything looks fine. Id in alert is correct but my database is not updated.

MYSQL How do I select data with both a WHERE EXISTS condition for another table and a normal WHERE condition in the main table?

mysql

The following code fetches data if the SubjectB column from Table 2 matches the Subject A column from Table 1.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.