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

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>

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.