/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] PHP remove Selected tags by id or class

here, i searched for this and i havent found or understood any similar solution to this problem.

in case i have this string

[code=html]
<div id=”to_remove”>&nbsp;</div>
<div id=”retain”>Do Not Remove</div>
<a class=”to_remove”>Remove</a><br/>
<div class=”to_retain”>This</div>
<input onclick=”remove this;”/>
[/code]

how can i remove the html tag with id and class `to_remove`. i mean the whole div tag. and how can i remove the `onclick`,`onload`, or any event javascript from html.

thank you for a kind response..

function would look like

[code=php]
function strip_selected_tags_by_id_or_class($array_of_id_or_class){
//logic…
}
[/code]

the output would be…

[code=html]
<div id=”retain”>Do Not Remove</div><br/>
<div class=”to_retain”>This</div>
<input />
[/code]

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@jasonahouleNov 06.2007 — It is in no way perfect but it should be easy enough to polish it and adjust it to your needs.

<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;/title&gt;
&lt;script type="text/javascript"&gt;
function removeItem(id) {
element = document.getElementById(id);
if(element == null) {
allElements = document.all ? document.all : document.getElementsByTagName('*');
count = allElements.length;
for(i=0; i&lt; count; i++) {
try {
allElements[i].className
if(allElements[i].className == id) {
remove(allElements[i]);
}
} catch (err) {
// no class name defined
}
}
} else {
remove(element);
}
}
function remove(element) {
if(element.tagName == 'INPUT') {
element.removeAttribute('onclick');
} else {
element.parentNode.removeChild(element);
}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id="parent"&gt;
This is the parent
&lt;div id="child"&gt;
This is the child
&lt;/div&gt;
&lt;div class="myClass"&gt;
This is the class
&lt;/div&gt;
&lt;/div&gt;
&lt;input type="button" value="remove id" onclick="removeItem('child')"/&gt;
&lt;input type="button" value="remove class" onclick="removeItem('myClass')"/&gt;
&lt;input type="button" value="remove event" onclick="removeItem('myButton')" class="myButton"/&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@bokehNov 06.2007 — I think he wants to clean this up with PHP before it is sent to the client.
Copy linkTweet thisAlerts:
@php_hazard_01authorNov 06.2007 — yes, i really need it in PHP..but i think javascript can suffice...thank you...im still looking for a PHP solution to this..

THank You
Copy linkTweet thisAlerts:
@bokehNov 07.2007 — Will there be any nested divs? Example:[code=html]<div id="to_remove"><div id="retain">Do Not Remove</div></div>[/code]or[code=html]<div id="to_remove"><div id="other_id">Child div</div></div>[/code]
Copy linkTweet thisAlerts:
@php_hazard_01authorNov 07.2007 — hmmm..another problem, okay, in this case, no nested divs. but i would like to see some implementation about removing outer divs and retaining the inner ones...


well, for this problem, no..no nested divs..thanks bokeh for presenting that..
Copy linkTweet thisAlerts:
@NogDogNov 07.2007 — [code=php]
function strip_selected_tags_by_id_or_class($array_of_id_or_class, $text)
{
$name = implode('|', $array_of_id_or_class);
$regex = '#<(w+)s[^>]*(class|id)s*=s*['"](' . $name .
')['"][^>]*>.*</\1>#isU';
return(preg_replace($regex, '', $text));
}
[/code]
Copy linkTweet thisAlerts:
@php_hazard_01authorNov 07.2007 — i love you nogdog!!!!!!!!!!............i really do!!!!.......


i wish i can understand regex like you do...it's like a different language...

havent started studying it yet..?

i'll try it out now..
Copy linkTweet thisAlerts:
@bokehNov 07.2007 — [code=php]
function strip_selected_tags_by_id_or_class($array_of_id_or_class, $text)
{
$name = implode('|', $array_of_id_or_class);
$regex = '#<(w+)s[^>]*(class|id)s*=s*['"](' . $name .
')['"][^>]*>.*</\1>#isU';
return(preg_replace($regex, '', $text));
}
[/code]
[/QUOTE]
Just a couple of things: First if you are going to use external data in your expression you need to use preg_quote on the data before turning it into an expression. Using the default delimiter will mean preg_quote doesn't need the second argument. Also I would use a backreference to match quote styles. [CODE]function strip_selected_tags_by_id_or_class($array_of_id_or_class, $text)
{
[COLOR="Red"][B]$array_quoted = array_map('preg_quote', $array_of_id_or_class);[/B][/COLOR]
$name = implode('|', [COLOR="Red"][B]$array_quoted[/B][/COLOR]);
$regex = '[COLOR="Red"][B]/[/B][/COLOR]<(w+)s[^>]*([COLOR="Red"][B]?:[/B][/COLOR]class|id)s*=s*[COLOR="Red"][B](['"])[/B][/COLOR]([COLOR="Red"][B]?:[/B][/COLOR]' . $name .
')[COLOR="Red"][B]2[/B][/COLOR][^>]*>.*</\1>[COLOR="Red"][B]/[/B][/COLOR]isU';
return(preg_replace($regex, '', $text));
}[/CODE]
Copy linkTweet thisAlerts:
@php_hazard_01authorNov 07.2007 — what do you mean?..sorry if i dont get this too much, im a newb in regex functions... sorry, i do not understand regex..LoL, please translate it in english,

sorry, i havent learned regex yet..i will later on..but for now, you guys are my only solution..
Copy linkTweet thisAlerts:
@bokehNov 07.2007 — please translate it in english[/QUOTE]What is it in particular about my use of English that you find difficult to understand?
Copy linkTweet thisAlerts:
@php_hazard_01authorNov 08.2007 — haha..LoL, nvermind, thanks anyway..dude, please check my other thread titled get id name of dynamically loaded list on the javascript section..
×

Success!

Help @php_hazard_01 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...