jwhite68 Posted October 1, 2007 Share Posted October 1, 2007 Does anyone know of a function to strip attributes from any HTML tag? I know strip_tags will remove the tags themselves, but this doesnt strip attributes for any specific tags you want to leave in your source. Quote Link to comment https://forums.phpfreaks.com/topic/71341-a-standard-function-to-strip-attributes-from-a-html-tag/ Share on other sites More sharing options...
jwhite68 Posted October 1, 2007 Author Share Posted October 1, 2007 The following comes close to achieving this: First I remove all tags except for ul,li and br from my HTML string ($desc). So I can keep a clients bullet point format, and line breaks at the very least. The preg_replace functions are meant to remove any attributes left, eg that may be present in the li tags. $output = strip_tags($desc,'<ul><li><br>'); $output = preg_replace("/<([^\s>]+)[^>]+\/>/i", "<\\1 />", $output); $output = preg_replace("@<([^\s>]+)[^>]+[^\/]>@si", "<\\1>", $output); The problem is that my clients source data does not put </li> tags after each li section. And I think this is causing the above to screw up. Because instead of putting the </li> and </ul> and the end, it just puts </></>. Can anyone advise? Quote Link to comment https://forums.phpfreaks.com/topic/71341-a-standard-function-to-strip-attributes-from-a-html-tag/#findComment-358979 Share on other sites More sharing options...
jwhite68 Posted October 1, 2007 Author Share Posted October 1, 2007 Someone posted the following function on a website, but it doesnt work. function strip_attributes($msg, $tag, $attr, $suffix=""){ /* $msg. The text you want to strip attributes from. $tag. The tag you want to strip attributes fom (p, for instancee). $attr. An array with the name of the attributes you want to strip (leaving the rest intact). If the array is empty, the function will strip all attributes. $suffix. An optional text to append to the tag. It may be a new attribute, for instance */ $lengthfirst = 0; while (strstr(substr($msg, $lengthfirst), "<$tag ") != "") { $tag_start = $lengthfirst + strpos(substr($msg, $lengthfirst), "<$tag "); $partafterwith = substr($msg, $tag_start); $img = substr($partafterwith, 0, strpos($partafterwith, ">") + 1); $img = str_replace(" =","=", $img); $out = "<$tag"; for($i=0; $i < count($attr); $i++) { if (empty($attr[$i])) { continue; } $long_val = (strpos($img, " ", strpos($img, $attr[$i] . "=")) === FALSE) ? strpos($img, ">", strpos($img, $attr[$i] . "=")) - (strpos($img, $attr[$i] . "=") + strlen($attr[$i]) + 1) : strpos($img, " ", strpos($img, $attr[$i] . "=")) - (strpos($img, $attr[$i] . "=") + strlen($attr[$i]) + 1); $val = substr($img, strpos($img, $attr[$i] . "=" ) + strlen($attr[$i]) + 1, $long_val); if (!empty($val)) { $out .= " " . $attr[$i] . "=" . $val; } } if (!empty($suffix)) { $out .= " " . $suffix; } $out .= ">"; $partafter = substr($partafterwith, strpos($partafterwith,">") + 1); $msg = substr($msg, 0, $tag_start). $out. $partafter; $lengthfirst = $tag_start + 3; } return $msg; } Can anyone understand why it doesnt work? Quote Link to comment https://forums.phpfreaks.com/topic/71341-a-standard-function-to-strip-attributes-from-a-html-tag/#findComment-359102 Share on other sites More sharing options...
effigy Posted October 1, 2007 Share Posted October 1, 2007 preg_replace('/<([a-z]+)[^>]*>/i', '<\1>', $input); Quote Link to comment https://forums.phpfreaks.com/topic/71341-a-standard-function-to-strip-attributes-from-a-html-tag/#findComment-359151 Share on other sites More sharing options...
jwhite68 Posted October 2, 2007 Author Share Posted October 2, 2007 Thanks effigy - that did resolve my particular test case. How sure are you that this will work as a general rule for all attributes? i.e. are you using this code in a production environment? Quote Link to comment https://forums.phpfreaks.com/topic/71341-a-standard-function-to-strip-attributes-from-a-html-tag/#findComment-360120 Share on other sites More sharing options...
effigy Posted October 2, 2007 Share Posted October 2, 2007 All HTML tag names consist of letters. Therefore, the pattern matches any number of consecutive letters and excludes anything afterwards up to the end of the tag. You may want an additional expression to remove tags that begin with <!. Quote Link to comment https://forums.phpfreaks.com/topic/71341-a-standard-function-to-strip-attributes-from-a-html-tag/#findComment-360128 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.