beranpa8
beranpa8

Reputation: 445

Deleting parentheses from string using regex

Hi,

I am trying to delete [] parentheses with whats inside of them from string in php.

Input: "Hello [string] world!" , expected output: "Hello world!"

What I tried is:

 `ereg_replace("\[[^\]]*\]","",$sres);` 

Where $sres is the string I am trying to clean up. This should work imo and for some weird reason it does little bit. It actually replaces "[1]" with "", but it does not replace for example "[edit]", nor "[""]" and so on. I even tried to wrap the regex in / / :

`ereg_replace("/\[[^\]]*\]/","",$sres); `

But this didnt work at all, not even on that "[1]". I would be very helpful for any help.

Upvotes: 0

Views: 31

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174844

You could use preg_replace.

preg_replace('~\[[^\]]*\]~', '', $sres);

DEMO

Upvotes: 3

Related Questions