I'm trying to use an array of regular expressions to find and replace within a string in PHP, however I'm getting the error unknown modifier
. I'm aware this appears to be a popular issue, however I don't understand how to fix it in my scenario.
Here is my original regex pattern:
{youtube((?!}).)*}
I run the following code against it to escape any characters:
$pattern = '/' . preg_quote($pattern) . '/';
That returns the following:
/\{youtube\(\(\?\!\}\)\.\)\*\}/
However, when I run this pattern through preg_replace
I get the following error:
Warning: preg_replace() [function.preg-replace]: Unknown modifier 'y' ...
Any idea what needs to be changed, and at what stage of the code I've show here?
Many thanks
Edit 1
As requested, here is the code I'm using:
$content = "{youtube}omg{/youtube}";
$find = array();
$replace = array();
$find[] = '{youtube((?!}).)*}';
$replace[] = '[embed]http://www.youtube.com/watch?v=';
$find[] = '{/youtube((?!}).)*}';
$replace[] = '[/embed]';
foreach ( $find as $key => $value ) {
$find[$key] = '/' . preg_quote($value) . '/';
}
echo preg_replace($find, $replace, $content);
Here's a live example