Jump to content

stripping specific span tags based on their id


gerkintrigg

Recommended Posts

Is there a way of removing a span from a string of HTML by using it's id?

 

I have this string:

$string='<span id="1">test string</a><span id="2">test string2</a>';

I would like to be able to dynamically remove the html from one or the other tag depending on the user input, which relates to the id.

 

I have tried where commands relating to the id, but it's just not the same as SQL...

 

I'd like something like this (I know the code doesn't work... but you get the idea):

strip_tags($string, where(id=$user_input))

Link to comment
Share on other sites

Whoops, I made and tested a solution to this problem but forgot to post it  :facepalm:

 

$str = '<span id="1">test string</span><span id="2">test string2</span>';
$input = '1';
$str = preg_replace("~<span id=\"$input\">(.+?)<\/span>~", "$1", $str);
echo $str; // test string<span id="2">test string2</span>

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.