Wilf
Wilf

Reputation: 2315

PHP : How to remove string between "[" and "]"

I need to remove string inside [...] including "[]" itself. I tried searching for a solution from this site. I have a clue that I should try something with preg_replace but it seems too expert to me.

For example :

[gallery ids="92,93,94,95,96,97,98,99,100" orderby="rand"] Description The thirty-two storey resort condominium in Phuket, located Just 150m from Patong beach where choices of activities, water sp 

I need to remove [gallery ids="92,93,94,95,96,97,98,99,100" orderby="rand"] from the example text. And it always begins with [gallery ids=" .

Please suggest.

Upvotes: 2

Views: 3782

Answers (1)

kittycat
kittycat

Reputation: 15045

Try this:

$string = '[gallery ids="92,93,94,95,96,97,98,99,100" orderby="rand"] Description The thirty-two storey resort condominium in Phuket, located Just 150m from Patong beach where choices of activities, water sp ';
$string = preg_replace('/\[gallery ids=[^\]]+\]/', '', $string);

Breakdown:

\[gallery ids= look for substring that begins with [gallery ids=

[^\]]+\] match 1 or more characters that are not ] until you reach a ]

Will then replace that whole matched portion with '' nothing and you have your new string.

Upvotes: 7

Related Questions