Jump to content

Escaping Square Brackets in preg_replace


scanreg

Recommended Posts

I know you need to escape square brackets in regular expressions:

 

\

\[\/url]

 

However, in the following preg_replace there is this:

 

$text = preg_replace('/\[b]([^[]+)\[\/b]/i',
'<strong>$1</strong>', $text);

My question is why aren't the square brackets inside the parens escaped?

 

Should they be like this instead:

 

$text = preg_replace('/\[b](\[^\[]+)\[\/b]/i', 
'<strong>$1</strong>', $text);

 

Many thanks  :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.