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

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • 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.