I have this code
function a($menu_item, $remove_link) {
$pattern = 'class="(.+)"(.+)<a.+>(.+)</a>';
if($remove_link) {
return preg_replace($pattern, 'class="$1 selected"$2$3', $menu_item); //<- line 6
}
return $menu_item;
}
Which basically checks if $remove_link
is true, and then removes the link and adds a class definition to $menu_item
For example, if i use
$menu_item = '<li class="menuitem first"><a href="index.php">Home</a></li>';
$menu_item = a($menu_item, true);
It should return
<li class="menuitem first selected">Home</li>;
The regex is tested and it works in Notepad++, but my function is giving this error:
Warning: preg_replace(): Delimiter must not be alphanumeric or backslash in functions.php on line 6
I saw that php patterns have to be "delimited" with slashes, so i tried to use class="/(.+)"(.+)<a.+>(.+)/</a>
instead, but it gives the same error.
What am i missing? How do i use delimiters properly?