0

Below is my string. Here i use the preg_replace

$marr = '<ul><li class="test_item test-item-28 current_test_item">'.
        '<a href="test/">Home</a></li><li class="test_item test-item-30">'.
        '<a href="test/about-us/">About Us</a></li>'.
        '<li class="test_item test-item-45"><a href="test/products/">Products</a></li>'.
        '<li class="test_item test-item-47"><a href="test/latest-news/">Latest News</a></li>'.
        '<li class="test_item test-item-49"><a href="test/contact-us/">Contact Us</a></li>'.
        '</ul>';

echo preg_replace('/<li class="test_item test-item-45">(.*)<\/li>/',
                  'test',
                  $marr);

While using this, it replace the given string. But the latest news and contact menus are missing . In the out put the last 2 li are missing. What is wrong here?

Please help me. Thanks

Itay
  • 16,601
  • 2
  • 51
  • 72
Sam Hanson
  • 1,317
  • 4
  • 22
  • 48
  • [Don't use regular expressions to parse HTML](http://stackoverflow.com/q/1732348/112968). The concrete problem you are facing is _greedy matching of input_. – knittl Sep 06 '13 at 07:27

1 Answers1

1

You should use (.*?)

echo preg_replace('/<li class="test_item test-item-45">(.*?)<\/li>/', 'test', $marr);

in order to break match after the first </li> occurred

Luca Rainone
  • 16,138
  • 2
  • 38
  • 52