代码
所有 $menu 值:将 class="*" 替换为 class="current"
$menuA = preg_replace('~class=".+"~', 'class="current"', $menu);
print_r($menuA);
所有 $menu 值:将 className "current" 添加到当前类
$menuB = preg_replace('~class="(.+?)"~', 'class="$1 current"', $menu);
print_r($menuB);
所选菜单:将 class="*" 替换为 class="current"
$menuC = preg_replace('~class=".*?page-item-'.$selectedMenuId.'"~', 'class="current"', $menu);
print_r($menuC);
选定菜单:将className“current”添加到当前类
$menuD = preg_replace('~class="(.*?)page-item-('.$selectedMenuId.')"~', 'class="$1page-item-$2 current"', $menu);
print_r($menuD);
输出
$menuA = Array
(
[0] => <li class="current">
[1] => <li class="current">
[2] => <li class="current">
[3] => <li class="current">
[4] => <li class="current">
)
$menuB = Array
(
[0] => <li class="page_item page-item-155 current">
[1] => <li class="page_item page-item-49 current">
[2] => <li class="page_item page-item-72 current">
[3] => <li class="page_item page-item-18 current">
[4] => <li class="page_item page-item-50 current">
)
$menuC = Array
(
[0] => <li class="page_item page-item-155">
[1] => <li class="page_item page-item-49">
[2] => <li class="current">
[3] => <li class="page_item page-item-18">
[4] => <li class="page_item page-item-50">
)
$menuD = Array
(
[0] => <li class="page_item page-item-155">
[1] => <li class="page_item page-item-49">
[2] => <li class="page_item page-item-72 current">
[3] => <li class="page_item page-item-18">
[4] => <li class="page_item page-item-50">
)