0
我需要一些幫助。我研究過正則表達式,但還沒有完全理解它的實現。如果父級包含給定的類或ID,我需要一個將刪除所有標籤及其子級的片段。PHP:刪除所有包含給定類別或標識的標籤
例子:
<?php
function remove_tag($find="",$html)
{
# Remove multiple #IDs and classes at once
# When given a string (separating objects with a comma)
if (is_string($find))
{
$objects = explode(',', str_replace(' ', '', $find);
} else if (is_array($find)) {
$objects = $find;
}
foreach ($objects as $object)
{
# If ID
if (substr($object,0,1) == '#')
{
# regex to remove an id
# Ex: '<ANYTAG [any number of attributes] id='/"[any number of ids] NEEDLE [any number of ids]'/" [any number of attributes]>[anything]</ENDTAG [anything]>'
}
if (substr($object,0,1) == '.')
{
# remove a class
# Ex: '<ANYTAG [any number of attributes] class='/"[any number of classes] NEEDLE [any number of classes]'/" [any number of attributes]>[anything]</ENDTAG [anything]>'
}
# somehow remove it from the $html variable?
}
}
很抱歉,如果這是一個新手的問題,感謝您的時間! :)
-Pat
如果你用正則表達式解析HTML,他會來http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – 2013-02-15 23:59:33
許多人向我證明,儘管我第一次不同意,但簡單的正則表達式並不是HTML解析的可靠方式。總會有毛病。因此,如果此解析的代碼是用戶輸入,請使用http://htmlpurifier.org/。 – 2013-02-15 23:59:57
嗯...你想刪除類或刪除元素? 那麼你想
成爲(通過刪除類classNumberOne)? 或者你想要具有給定類的元素,只是爲了隱藏?如果沒有完全理解你想實現什麼,那麼我可能會在javaScript中完成它(如果我已經理解了你想要的)。沿着getElementById的行並放置樣式顯示:none;在上面。對不起,如果我誤解了你的問題...我試過了。 :-) – Zeth 2013-02-16 00:01:55