We have a variable with hmtl code inside.
<?php echo $list; ?>
This will give something like:
<li><a href='http://site.com/2010/' title='2010'>2010</a></li>
<li><a href='http://site.com/2009/' title='2009'>2009</a></li>
<li><a href='http://site.com/2008/' title='2008'>2008</a></li>
Want to add class for each <li>, it can be taken from title attribute:
<li class="y2010"><a href='http://site.com/2010/' title='2010'>2010</a></li>
<li class="y2009"><a href='http://site.com/2009/' title='2009'>2009</a></li>
<li class="y2008"><a href='http://site.com/2008/' title='2008'>2008</a></li>
We should work with variable $list.
Tentative scheme:
- search for
titleattribute in each
<li>....</li> - throw its value to the class, which we add for opening
<li>
PHP solution wanted.
Thanks.
Parsing the DOM sounds like overkill to me, if I understand the problem you’re facing. Assuming that you know for sure that the entire contents of the
$listvariable will be structured as<li><a href='foo' title='bar'>bar</a></li>then you can do what you’re asking pretty easily by combining regular expressions with a loop:If you echo $output: