How to remove section of text from $string

mrb1972

Active member
Hi,

PHP question. Is it possible to remove a section of text from a string ?

lets say I have a string $example="[caption id="text"] I want to remove this text [/caption] But I want to keep this bit"

And i want to strip out "[caption id="text"] I want to remove this text [/caption]" from the string, how is this done?

thanks
 
Are you wanting to replace it with html code, or just remove it completely? Also, if you used that code it'd give errors because you have a double quote inside of a double quote.
 
Are you wanting to replace it with html code, or just remove it completely? Also, if you used that code it'd give errors because you have a double quote inside of a double quote.

Just remove the code completly, (I know the quote thing shouldnt be there, it was just an example )
 
Just use str_replace with no replace parameter.
PHP:
str_replace("I want to remove this text", "", $example);
 
I want to remove everything between [caption & /caption] (including [caption /caption]), I have many text records in my database that have these randomly throughout and each contains different text, I want to automatically go through each text record and strip out [caption /caption] code and all text inbetween
 
Off the top of my head....
Code:
$example = preg_replace('#\[caption id=".+?"\].*?\[/caption\]#i', '', $example)
 
Top Bottom