Regular Expression remove [caption]

I am trying to remove some html from my string of text which comes from a WordPress generated database.

I want this:

Read More
Marnie Stanton led us through the process first and then everyone went crazy. 
 One of the work stations
 The group is getting some great results
 You can see the prints multiplying  

to turn into this:

Marnie Stanton led us through the process first and then everyone went crazy. 

So what I want is everything from the first to the very last to be removed.

I have started with this:

([captions+?[^]]+])

Which only removes the first tag.

Related posts

Leave a Reply

4 comments

  1. You may want to use something like this

    $string = 'Marnie Stanton led us through the process first and then everyone went crazy. 
    [caption id="attachment_76" align="alignnone" width="191"] One of the work stations[/caption]
    [caption id="attachment_78" align="alignnone" width="300"] The group is getting some great results[/caption]
    I want to keep this !
    [caption id="attachment_83" align="alignnone" width="224"] You can see the prints multiplying[/caption]';
    
    $new_string = preg_replace('#s*[caption[^]]*].*?[/caption]s*#is', '', $string);
    echo $new_string;
    

    Output:

    Marnie Stanton led us through the process first and then everyone went crazy.I want to keep this !

    Explanation:

    • Modifiers is : i means match case insensitive, s means match new lines with dots .
    • s* : match white spaces 0 or more times
    • [caption : match [caption
    • [^]]* : match anything except ] 0 or more times
    • ] : match ]
    • .*?[/caption] : match anything until [/caption] found (and match [/caption])
    • s* : match white spaces 0 or more times

    Online demo

  2. As it seems you just want the start of the string, I would not use a regular expression but string functions:

    $pos = stripos($your_string, '[caption');
    $result = substr($your_string, 0, $pos);
    
  3. [caption] is an example of a shortcode. You can remove all shortcodes with WordPress’ strip_shortcodes(); function.

    $text = 'Marnie Stanton led us through the process first and then everyone went crazy. 
    [caption id="attachment_76" align="alignnone" width="191"] One of the work stations[/caption]
    [caption id="attachment_78" align="alignnone" width="300"] The group is getting some great results[/caption]
    I want to keep this !
    [caption id="attachment_83" align="alignnone" width="224"] You can see the prints multiplying[/caption]';
    
    $text = strip_shortcodes($text);
    echo $text;
    

    This will output:

    Marnie Stanton led us through the process first and then everyone went crazy.I want to keep this !


    [caption] documentation

    strip_shortcodes documentation

  4. It seems like you can explode the string by newlines, and just take the first line…

    <?php
    
    $str = <<<EOD
    Marnie Stanton led us through the process first and then everyone went crazy.
    [caption id="attachment_76" align="alignnone" width="191"] One of the work stations[/caption]
    [caption id="attachment_78" align="alignnone" width="300"] The group is getting some great results[/caption]
    [caption id="attachment_83" align="alignnone" width="224"] You can see the prints multiplying[/caption]
    EOD;
    
    $lines = explode("n", trim($str));
    
    echo $lines[0]; # Marnie Stanton led us through the process first and then everyone went crazy.