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. Link to comment https://forums.phpfreaks.com/topic/94228-removing-from-string-with-wildcard/ 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 // ) ?> Link to comment https://forums.phpfreaks.com/topic/94228-removing-from-string-with-wildcard/#findComment-482669 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); Link to comment https://forums.phpfreaks.com/topic/94228-removing-from-string-with-wildcard/#findComment-482681 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.