Hey, Would you like to work at Home ?? Just click here No need to pay, just register free and activate your account and get data Entry Work at your Home.

Wednesday, August 15, 2007

function strip attributes

Most website owners realise that using a CMS can be beneficial to their website but very few know what these benefits actually are. Knowing the specific benefits offered by a CMS can help you to better decide which is most suitable for your website.
With the abundance of news and blog (Weblog) sites growing continuously, keeping track of what’s going on (and adding iy to your site) can be a daunting task. Fortunately, standards such as RSS (Really Simple Syndication) provide an easy way to grab content from a particular site and aggregate it into a CMS application. This means that, rather than looking for news yourself, some piece of software monitors sites that you’re interested in and downloads new content as it’s published. Although this can alleviate the problem of adding content to a website, it raises the issue of keeping the article format consistent.
PHP provides some functions to strip unwanted formatting away from a piece of text (strip_tags(), for instance). However, many sources embed formatting information in the form of tag attributes (style, font, etc.). This formatting information is usually embedded in “common” tags, such as P, SPAN,TABLE, etc. Most users will want to keep these tags because they provide the basic text structure but they want to avoid any formatting information (which is most likely provided by a CSS stylesheet). So how can attributes be stripped? The following function allows stripping all attributes from any tag (instead of the whole tag), thus leaving the content structure intact.

===========================================================================

function strip_attributes($msg, $tag, $attr, $suffix = “”)

{


$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;

}
===========================================================================

The function takes 4 parameters.

1. $msg. The text you want to strip attributes from.
2. $tag. The tag you want to strip attributes fom (p, for instancee).
3. $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.
4. $suffix. An optional text to append to the tag. It may be a new attribute, for instance.

This function can be used as an article filter, cleaning the articles before storing them into the CMS database. This can also be used to clean user-entered input to prevent the execution of Javascript events (onmouseclick, for instance) or to delete any style or font attributes from the text.

1 comment:

Sachin Kumar Rajput said...

thanks for the intelligent code, I was looking exactly for the same ;)

Your Ad Here