Search code examples
preg-replaceereg-replace

could someone help me with my ereg_replace converted to preg_replace


I just need a bit of help with my ereg_replace changed to preg_replace..

ereg_replace('<caption.*</caption>', '', $match);

and I've tried

preg_replace('/<caption.*</caption>/', '', $match);

but it doesn't work.. and it says "Warning: preg_replace(): Unknown modifier 'c'"

I'm new to this kinda thing.. so any help would be appreciated :)


Solution

  • The 'c' in question is the one in </caption> in your original regex. When the parser sees the /, it assumes that it's an ending delimiter, the regex is over, and it's looking for modifier flags. Not recognizing a modifier flag called c it throws the error you're seeing.

    So you could fix things by escaping the slashes. In my mind, though, it might be more convenient to use a non-slash character (I'm partial to #) as your delimiter:

    preg_replace('#<caption.*</caption>#', '', $match);