• kosmonaft

    (@kosmonaft)


    Hello everyone,

    I am creating a very simple WordPress page (5 pages).
    The template is pretty simple but every single page have a different combination of boxes and information.

    That’s why I decided to prepare 5 pages (without any posts).
    By default every page/post have Title and Body(content) Field.

    To make it simpler I am trying to write the Body in this way:

    [TOP PART]This is subtitle[/TOP PART]
    [BOX]This is my box 1[/BOX]
    [BOX]This is my box 2[/BOX].

    in my index.php I am trying to filter it with preg_match_all:

    $string =  apply_filters( 'the_content', get_post_field( 'post_content', get_option( 'page_for_posts' ) ) );
    
    $preg = preg_match_all('/\[BOX\](?:(.+?)?\[\/BOX\])/', $string , $matches);
    
    var_dump($matches);

    Unfortunately I received an empty Array. If I display the $string I see the content with this boxes.

    Any idea why preg_match_all() doesn’t work with content of the post/page?

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Starting with your code, I set $string equal to your box content because I’m testing outside the loop. The rest of your code is identical and I get this output:

    array (size=2)
      0 =>
        array (size=2)
          0 => string '[BOX]This is my box 1[/BOX]' (length=27)
          1 => string '[BOX]This is my box 2[/BOX]' (length=27)
      1 =>
        array (size=2)
          0 => string 'This is my box 1' (length=16)
          1 => string 'This is my box 2' (length=16)

    So there is no problem with preg_match_all(), thus either improper content is returned from filters or there’s something very queer with your PHP installation. Probably the first?

Viewing 1 replies (of 1 total)
  • The topic ‘preg_match_all doesnt't work with content’ is closed to new replies.