RavenStar Posted March 4, 2008 Share Posted March 4, 2008 Hi all. I got a fairly simple question, which I can't seem to work out after a few attempts at different approaches and searching the web. Basically I have an array which contains something along the lines of; <li class="cat-item cat-item-139"> <a href="blahblah.com">Title</a> at the start of every of every occurrence, though the number is always different. I want to remove the whole <li> tag, so I am only left with the <a> tag. How can I go about replacing/removing everything between point A and point B? Thank you in advanced. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted March 4, 2008 Share Posted March 4, 2008 If only the number changes in <li class="cat-item cat-item-139">, then: <?php $array = array('<li class="cat-item cat-item-7"> <a href="blahblah.com">Title</a> sdf sf sdfsd', '<li class="cat-item cat-item-31"> <a href="blahblah.com">Title</a> sdf sf sdfdsad asd d', '<li class="cat-item cat-item-3212"> <a href="blahblah.com">Title</a> dad dsad dsdd'); $newArray = preg_replace('(<li class="cat-item cat-item-[0-9]+"> )', '', $array); // Array // ( // [0] => <a href="blahblah.com">Title</a> sdf sf sdfsd // [1] => <a href="blahblah.com">Title</a> sdf sf sdfdsad asd d // [2] => <a href="blahblah.com">Title</a> dad dsad dsdd // ) ?> Quote Link to comment Share on other sites More sharing options...
thebadbad Posted March 4, 2008 Share Posted March 4, 2008 If the string we're removing occurs more than once in an array value, you want to put a limit of 1 in the preg_replace. Change the line in question to this: $newArray = preg_replace('(<li class="cat-item cat-item-[0-9]+"> )', '', $array, 1); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.