Jump to content
php.lv forumi

neliels regex helps vajadzīgs


artis

Recommended Posts

Ir parasta menu struktūra:

<ul>
 <li>txt</li>
 <li>txt
<ul>
 	<li>txt</li>
 	<li>txt</li>
</ul>
 </li>
</ul>

 

Man vajag pēdējo "</ul>" aizstāt ar "<li class="klasse"><a href="#">Quick</a></li></ul>" ...(gatavam menu pielikt beigās vēl vienu menu item).

Izveidoju sekojošu kodu, ar norādi "$", lai tiek ņemts tikai pēdējais "</ul>":

$menu = preg_replace('/(<\/ul>)$/', '<li class="klasse"><a href="#">Quick</a></li></ul>', $menu );

, bet kaut kā nekas nenotiek un nevaru saprast, kas vēl vajadzīgs...

Edited by artis
Link to comment
Share on other sites

Kaut kāda patizla atbilde, bet ja nu kādam ko līdzīgu vēl kādreiz vajag, bez regex`iem:

function str_replaceLast($s,$r,$str) {
$strArr = explode($s,$str);
if(count($strArr) <= 1) {
 return $str;
}
$newStr = array_pop($strArr);
$str = implode($s,$strArr);
$str .= $r.$newStr;
return $str;
}

Edited by artis
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...