Jump to content

Removing from string with wildcard?


RavenStar

Recommended Posts

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

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
// )
?>

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.