gerkintrigg Posted November 22, 2009 Share Posted November 22, 2009 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)) Quote Link to comment https://forums.phpfreaks.com/topic/182507-stripping-specific-span-tags-based-on-their-id/ Share on other sites More sharing options...
FaT3oYCG Posted November 22, 2009 Share Posted November 22, 2009 preg_replace is what you need then just wrap a function arround it much like the example you provided minus the where() change that for $id and then check for that in the preg string. Quote Link to comment https://forums.phpfreaks.com/topic/182507-stripping-specific-span-tags-based-on-their-id/#findComment-963269 Share on other sites More sharing options...
gerkintrigg Posted November 22, 2009 Author Share Posted November 22, 2009 I don't really understand how that would work? something like: preg_replace('<span id="$user_input"?>?</span>',strip_tags(<span id="$user_input"?>?</span>)) ??? Quote Link to comment https://forums.phpfreaks.com/topic/182507-stripping-specific-span-tags-based-on-their-id/#findComment-963296 Share on other sites More sharing options...
Alex Posted November 22, 2009 Share Posted November 22, 2009 Whoops, I made and tested a solution to this problem but forgot to post it $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> Quote Link to comment https://forums.phpfreaks.com/topic/182507-stripping-specific-span-tags-based-on-their-id/#findComment-963299 Share on other sites More sharing options...
gerkintrigg Posted November 23, 2009 Author Share Posted November 23, 2009 this post has been immensely useful. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/182507-stripping-specific-span-tags-based-on-their-id/#findComment-964201 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.