Removing WP tag with PHP

2

Colleagues.

We are creating a new system and removing the WP, but the news we still get from WP, like old news. But when we bring the column post_content, it brings me the tag that appears next to the image:

  

[caption id="attachment_9472" align="alignleft" width="350"] (here   the image appears) [/ caption]

I tried to use strip_tags, but it did not work. We are picking up without using WP, but our own system. How do I remove these tags and leave only the image?

I'm getting it this way:

<?php echo utf8_encode(strip_tags($jmVisualizar->post_content),'[caption]'); ?>
    
asked by anonymous 07.05.2016 / 21:09

1 answer

5

Using RegEx, this is one of the simplest ways:

preg_replace('/\[\/?caption.*\]/U', '', $string )

Basically, it changes everything inside [ ] and starts with caption or /caption . The /U f modifier is used to grab the smallest possible bit, so that the .* pope does not get things between two different tags.

If you want to remove all tags with [ ] the situation may even simplify:

preg_replace('/\[.*\]/U', '', $string );
                           ^--- aqui você põe um espaço se não for para "colar" tudo.

If you need to do something different with the parts before and after caption :

preg_replace( '/(.*)\[caption.*\](.*)\[\/caption\](.*)/', '$1$2$3', $string );

If you do not need the outside of the tag, and you just want the content :

preg_replace( '/.*\[caption.*\](.*)\[\/caption\].*/', '$1', $string );

If you want to remove the tag and content :

preg_replace( '/.*\[caption.*\].*\[\/caption\].*/', '', $string );

See all tests running IDEONE .


There are a thousand other ways to solve, it all depends on small details of the variations that your data may have.

    
07.05.2016 / 23:38