How to split <p> text text text </p> into array

I like to output and style in a shortcode, but the content that is get is the content of a page, so it’s style like that

<p>2012-12-12</p>
<p>2012-6-23</p>
<p>2012-7-3</p>

i like to be able to have ONLY the value date in a array to be able to output it in a unorder list after

Read More

how do i do that (strip the p and get it into an array ?

some code :

//Add a SHORTCODE to get date listing
add_shortcode ('getdate','get_date_listing');
   function get_date_listing ($att) {
       $req_id = 901;
       $post = get_page($req_id); 
       $content = apply_filters('the_content', $post->post_content);

       $contentarray = explode( 'n', $content );
       echo ($contentarray[0]);
       //var_dump ($contentarray);
       //return $content;
   } 

Related posts

Leave a Reply

2 comments

  1. Firstly, you need to remove the paragraph tags that were added by the wp_autop filter. There’s another answer that covers this pretty well: Is there an uw-wp_autop function?

    I’m going to change the function a little for our purposes (based on the markup example you gave):

    function reverse_wpautop( $s ) {
        // Strip newlines
        $s = str_replace( "n", "", $s );
    
        // Strip all <p> tags
        $s = str_replace( "<p>", "", $s );
    
        // Replace </p> with a known delimiter
        $s = str_replace( "</p>", "::|::", $s );
    
        return $s;
    }
    

    If all is working correctly, this should convert your markup from:

    <p>2012-12-12</p>
    <p>2012-6-23</p>
    <p>2012-7-3</p>
    

    To:

    2012-12-12::|::2012-6-23::|::2012-7-3::|::
    

    If you do a split now, you’ll end up with an extra, empty element in your array. So remember to take a substring before you split:

    function split_delimited_string( $s ) {
        // Take a substring, removing the final 5 characters (::|::)
        $s = substr( $s, 0, -5 );
    
        return explode( "::|::", $s );
    }
    
  2. here is the final working code :

       //Add a SHORTCODE to get date listing
    add_shortcode ('getdate','get_date_listing');
       function get_date_listing ($att) {
    
        $outputtvar = '';
    
        // set the default timezone to use. Available since PHP 5.1
        date_default_timezone_set('America/Montreal');
    
        //ID of the post containing DATE LIST
        $req_id = 901;
        $post = get_page($req_id); 
        $content = apply_filters('the_content', $post->post_content);
    
        // Strip all <p> tags
        $content = str_replace( "<p>", "", $content );
    
        // Replace </p> with a known delimiter
        $content = str_replace( "</p>", "|", $content );
    
        //Separate de dates
        $contentarray = explode( '|', $content );
    
        //remove the last empty date
        unset($contentarray[count($contentarray)-1]);
    
        if (qtrans_getLanguage() == 'fr') { setlocale(LC_ALL, 'fr_CA'); $datesetting = "%A, %e %B, %G"; } 
        if (qtrans_getLanguage() == 'en') { setlocale(LC_ALL, 'en_US'); $datesetting = "%A, %B %e, %G";} 
    
        //prepare the outputt
        $outputtvar .= '<ul>';
    
        foreach ($contentarray as $key => $value) {
            $timestamp = strtotime($value);
            $localdate = strftime($datesetting,$timestamp);
            $localdate = utf8_encode($localdate);
            $outputtvar .= '<li>' . $localdate . '</li>';
        }
    
        $outputtvar .= '</ul>';
    
        return $outputtvar ;
    
       }