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.

7
  • Have you considered using an XML parser for whatever it is you're doing? Commented Oct 2, 2012 at 20:02
  • Just replace -X" with -X current", where X is current selected menu. No need to replace class names. Commented Oct 2, 2012 at 20:04
  • @glavić That should be an answer. Commented Oct 2, 2012 at 20:07
  • @deizel: I am not sure that this is what eh wants. Variable $menu is array, so does he wants to replace every value? Or just the selected $menuId ? Commented Oct 2, 2012 at 20:11
  • @glavić I presume he has the ID, so just your str_replace in a foreach? (using $id instead of X) Commented Oct 2, 2012 at 20:13

2 Answers 2

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">
)
Sign up to request clarification or add additional context in comments.

3 Comments

NP. Next time be more specific in the question.
if $menu will be string not array and I want to replace just in the first tag li, How can I convet it to class="current" ? Thanks .
Just add 1 in the 4th parameter of the preg_replace() function, no matter if $menu is string or array.
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);

4 Comments

.. or just \d+ (if you are doing a comparison with the matching number).
@deizel: he wants to replace page_item page-item- too
True, but there is probably no need when the extra class can just be added. :)
@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! ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.