Wednesday, May 16, 2012

Conditional str_replace for pagination

H everyone,



I have the following:



$html = '<ul class="pagination-all">';
$html .= '<li class="pagination-start"><div class="img-start"></div>'.$list['start']['data'].'</li>';
$html .= '<li class="pagination-prev"><div class="img-prev"></div>'.$list['previous']['data'].'</li>';
$html .= '<ul class="pagination-data">';
foreach($list['pages'] as $page) {
$html .= '<li>'.$page['data'].'</li>';
}
$html .= '</ul>';
$html .= '<li class="pagination-next">'. $list['next']['data'].'<div class="img-next"></div></li>';
$html .= '<li class="pagination-end">'. $list['end']['data'].'<div class="img-end"></div></li>';
$html .= '</ul>';

return $html;


I was wondering how to make it so if it finds Start , string replaces "img-start" to "img-start-active". If it finds span class="pagenav"> End , string replaces "img-end" to "img-end-active".



I attempted:



$string = $html;
if(strstr($string, '<span class="pagenav"> Start </span>'))
{
echo str_replace("img-start", "img-start-active", $string);
}
else
{
echo " ";
}


Unfortunately, I don't believe I used this properly. What ended up was - while I got the desired string replacement given the condition, it echoed the whole pagination but without the formatting.



This is the image:enter image description here



So it was a mess.



Is there a way to do this search and replace but without creating a second "pagination"?





No comments:

Post a Comment