• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

How to remove section of text from $string

mrb1972

Active member
#1
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
 
#2
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.
 

mrb1972

Active member
#3
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 )
 

James

Well-known member
#4
Just use str_replace with no replace parameter.
PHP:
str_replace("I want to remove this text", "", $example);
 

mrb1972

Active member
#5
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
 

Jaxel

Well-known member
#9
Off the top of my head....
Code:
$example = preg_replace('#\[caption id=".+?"\].*?\[/caption\]#i', '', $example)