开发者

Replace text in PHP

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.


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>


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> 


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜