عرض مشاركة واحدة
عضو نشيط
تاريخ التسجيل: Sep 2006-
#1 (permalink)  
[مطورة] دالة stripAttribs لحذف الـ attributes من وسوم HTML


السلام عليكم,

كنت قد وضعت الدالة في أحد الردود داخل هذا الموضوع
مساعدة في دالة الاستبدال eregi_replace - سوالف سوفت

ولكن قمت بتطويرها لتلافي بعض المشاكل ولم أستطع التعديل علي الدالة في الموضوع الأصلي.

كذلك قمت بوضع بعض التعليقات داخل الدالة.

لذلك أرجو من الاخوان الذين قاموا بتنزيل الدالة نسخ الدالة المطورة من هنا

كود PHP:
/**
 * Function to strip unwanted HTML tag attributes
 * while allowing some attributes for each tag.
 * 
 * Example:
 * <code>
 * <?php
 
$string stripAttribs($string, array('img' => 'src,alt'));
 * 
?>
 * </code>
 * This line of code will strip all HTML tag attributes found in
 * the variable $string except for "src" and "alt" attributes in
 * the "img" HTML tag.
 *
 * @author Ahmed Mansi <dev.ahmed.mansi@gmail.com>
 * @copyright Copyright (c) 2008, Ahmed Mansi
 * @param string $text The string to search in
 * @param array $allowedAttribs optional Array of allowed attributes
 * @return string
 *
*/
function stripAttribs($text, $allowedAttribs = array())
{
    // get all HTML tags containing attributes
    preg_match_all('#<([a-zA-Z]+?)\s+?.*>#Us', $text, $allTags);
    
    for ($i = 0; $i < count($allTags[0]); $i++) {
        // enter loop if we are to allow some attributes for current tag
        if (array_key_exists($allTags[1][$i], $allowedAttribs)) {
            $allowedTagAttribs = str_replace(' ', '', $allowedAttribs[$allTags[1][$i]]);
            $allowedTagAttribs = str_replace(',', '|', $allowedTagAttribs);
            
            $pattern = '(?:' . $allowedTagAttribs . ')\s*=\s*'
                     . '(?:"[^"]*"|\'[^\']*\'|[^>\s]*)';
                     
            $cleanNewlines = str_replace(array("\r\n", "\r", "\n"), ' ', $allTags[0][$i]);
                     
            preg_match_all("#$pattern#i", $cleanNewlines, $matches);
            
            $strippedTag = '<' . $allTags[1][$i] . ' ' . implode(' ', $matches[0]);
        } else { // strip all tags (by returning just the tag name)
            $strippedTag = '<' . $allTags[1][$i];
        }
        
        // add a slash in the end of no-end-tag tags
        $noEndTags = array(
            'br', 'meta', 'img', 'hr', 'frame', 'base',
            'input', 'param', 'col', 'area', 'link'
        );
        $strippedTag .= in_array($allTags[1][$i], $noEndTags) ? ' />' : '>';
        
        // replace the original tag with the stripped one
        $text = str_replace($allTags[0][$i], $strippedTag, $text);
    }
    return $text;
}
كيفية استخدام الدالة:

الدالة تأخد بارامترين:
الأول: النص الذي ستعالج وسوم HTML بداخلة
الثاني هو الـ Attributes المسموح بتواجدها داخل وسوم الـ HTML. بمعني أننا سنترك هذه الوسوم ولن نحذفها.
والبارامتر عبارة عن مصفوفة لكل عنصر فيها :
- مفتاح: اسم وسم الـ html
- قيمة : الـ Arrtibutes المسموحة له مفصول بينها ب comma مثل "href,id"

والبارامتر الثاني اختياري يمكن عدم تمريره للدالة

مثال علي الاستخدام

كود PHP:
 $string stripAttribs($string, array('img' => 'src,alt')); 
ملحوظة: اذا كانت magic_quotes مفعلة بالسيرفر يجب التأكد من استخدام دالة stripslashes علي النص قبل تمريره الي الدالة.

أرجو أن تفيدكم






التصميم غير متواجد حالياً   قديم 14-04-2008, 01:27 AM
رد مع اقتباس