mardi 31 mars 2015

How to replace the last HTML list item?

How do I replace the last li element in this string?


Before:



$var =
'<li>anything<li/>
<li>anything<li/>
<li>anything<li/>
<li>anything<li/>
<li>anything<li/>';


After:



$var =
'<li>anything<li/>
<li>anything<li/>
<li>anything<li/>
<li>anything<li/>
<li class="foo">anything<li/>';


I tried with preg_replace():



preg_replace('<li>', '<li class="foo">', $var);


But it isn't the expected output. How do I change the code to get the expected output?


Aucun commentaire:

Enregistrer un commentaire