tags:

views:

87

answers:

4

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 title attribute in each <li>....</li>
  • throw its value to the class, which we add for opening <li>

PHP solution wanted.

Thanks.

+3  A: 

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 $list variable 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:

$list = "<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>";
preg_match_all("/title='([^']*)'/s",$list,$matches); //this gets all titles
$output=$list;
foreach($matches[1] as $match) { //this applies the titles to the li elements
  $location = strpos($output,"<li>");
  $output = substr($output,0,$location)."<li class='".$match."'>".substr($output,$location+4);
}

If you echo $output:

<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>
JGB146
+1  A: 

I accomplished this by splitting the text into an array, and performing a search/replace once the year is obtained.

    $carrReturn="\r\n"; //Set the Newline and Return string to search for
    $arr = explode($carrReturn, $list); //Break the text into an array
    $list=""; //clear $list
    for ($x=0; $x<count($arr); $x++){
            $current=$arr[$x];
            $year= strip_tags($current); //Get the year by stripping the HTML tags.

            $list.=str_replace("<li", "<li class=\"y".$year."\"",$current)."\r\n";      
            //Reconstruct $list
    }

Output

<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> 
Dutchie432
This works essentially how JGB146's code works, but his is a fair amount cleaner. If I saw his post first, I would have not posted this.
Dutchie432
A: 

RegEx:

preg_replace("/<li>(<a .+ title=')(\d{4})'/", "<li title='y$2'>$1$2", $string);

This really depends on every li and anchor being formatted the same exact way each time though.

joshtronic
+1  A: 

I dont know why you guys are so obsessed with Regex. DOM is clean and readable:

$dom = new DOMDocument;
$dom->loadXML("<ul>$list</ul>");
$xPath = new DOMXPath($dom);
foreach($xPath->query('//li/a/@title') as $node) {
    $node->parentNode->parentNode->setAttribute('class', $node->nodeValue);
}
echo $dom->saveXML($dom->documentElement);

Outputs:

<ul>
<li class="2010"><a href="http://site.com/2010/" title="2010">2010</a></li>
<li class="2009"><a href="http://site.com/2009/" title="2009">2009</a></li>
<li class="2008"><a href="http://site.com/2008/" title="2008">2008</a></li>
</ul>
Gordon
Regex would have been my immediate choice for that problem and I would never have considered using DOMDocument. +1 for opening my eyes a bit!
Steve Claridge