Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

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;
   } 
share|improve this question
    
I am thinking about using the : preg_split() but it's regular expression, and it get me sick –  menardmam Oct 3 '12 at 17:53
    
I have try : $DOM = new DOMDocument; DOM->loadHTML($content); $items = $DOM->getElementsByTagName('<p>'); but $items are empty ! :-( –  menardmam Oct 3 '12 at 18:17

2 Answers 2

up vote 1 down vote accepted

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 );
}
share|improve this answer
    
work fine, but i have in the array en extra empty value, at the end, why ? –  menardmam Oct 3 '12 at 18:25
    
See my edit. You need to strip off the final ::|:: delimiter before splitting the string, otherwise the closing </p> tag is assumed to be another element break. –  EAMann Oct 3 '12 at 18:32

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 ;

   } 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.