0

I have variable $menu type array like below:

$menu = array('<li class="page_item page-item-155">',
              '<li class="page_item page-item-49">',
              '<li class="page_item page-item-72">',
              '<li class="page_item page-item-18">',
              '<li class="page_item page-item-50">');

I want to replace the first original string

<li class="page_item page-item-155">'

to

<li class="current">

In the end of the string it can be any number, the numbers are not constant.

I think I need to use a regular expression but I don't know how to implement it.

$menu = str_replace($original_strs_array, $replacement_strs_array, $menu);

How can I do that? Thanks.

SethO
  • 2,703
  • 5
  • 28
  • 38
yossi
  • 1,431
  • 2
  • 11
  • 16

2 Answers2

3

Code

All $menu values: replace class="*" with class="current"

$menuA = preg_replace('~class=".+"~', 'class="current"', $menu);
print_r($menuA);

All $menu values: add className "current" to the current class

$menuB = preg_replace('~class="(.+?)"~', 'class="$1 current"', $menu);
print_r($menuB);

Selected menu: replace class="*" with class="current"

$menuC = preg_replace('~class=".*?page-item-'.$selectedMenuId.'"~', 'class="current"', $menu);
print_r($menuC);

Selected menu: add className "current" to the current class

$menuD = preg_replace('~class="(.*?)page-item-('.$selectedMenuId.')"~', 'class="$1page-item-$2 current"', $menu);
print_r($menuD);

Output

$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">
)
Glavić
  • 42,781
  • 13
  • 77
  • 107
1

try this :

preg_replace('/page_item page-item-\d+/','current',$menu);

Or this for a generic class remover/replacer:

preg_replace("/class\s*=\s*('|\")[^'\"]*('|\")/",'class="current"',$menu);
Ariyan
  • 14,760
  • 31
  • 112
  • 175
  • .. or just `\d+` (if you are doing a comparison with the matching number). – deizel. Oct 02 '12 at 20:06
  • @deizel: he wants to replace `page_item page-item-` too – Ariyan Oct 02 '12 at 20:11
  • 1
    True, but there is probably no need when the extra class can just be added. :) – deizel. Oct 02 '12 at 20:14
  • @deizel: Yes he can add class by Javascript and override CSS (if its needed) and forget all these. but he asked a PHP replacement question and I answered like that! ;-) – Ariyan Oct 02 '12 at 20:16