Как вырезать только img тэги?

Вот так сейчас все вырезаю.

{$oTopic->getTextShort()|strip_tags}


Как можно сделать, img вырезались, а остальные оставались?

6 комментариев

avatar
— Гм. Никак. — улыбнулся Orhideous. — Видите ли, модификатор Smarty strip_tags реализован плагином. Прошарьтесь в папке с плагинами, найдете его.
А потом сделайте свой, с понями и аликорнами. Но оригинальный обязательно оставьте.
avatar
Умничка!

Нашёл. Вот весь файл функции strip_tags. Но я не знаю, как можно сделать исключение по тэгу ?

function smarty_modifiercompiler_strip_tags($params, $compiler)
{
   if (!isset($params[1])) {
        $params[1] = true;
    }
    if ($params[1] === true) {
        return "preg_replace('!<[^>]*?>!', ' ', {$params[0]})";
    } else {
        return 'strip_tags(' . $params[0] . ')';
    }
}

?>
avatar
Вот:

<?php 
/** 
 * Smarty plugin 
 * @package Smarty 
 * @subpackage plugins 
 */ 


/** 
 * Smarty strip_tags modifier plugin 
 * 
 * Type:     modifier
 
 * Name:     strip_tags
 
 * Purpose:  strip html tags from text 
 * @link http://smarty.php.net/manual/en/language.modifier.strip.tags.php 
 *          strip_tags (Smarty online manual) 
 * @author   Monte Ohrt <monte at ohrt dot com> 
 * @author  Jordon Mears <jordoncm at gmail dot com> 
 * @author  Tekin Birdüzen <t.birduezen at web-coding dot eu> 
 * 
 * @version 3.0 
 * 
 * @param string 
 * @param boolean optional 
 * @param string optional 
 * @return string 
 */ 
function smarty_modifier_strip_tags($string) 
{ 
    // HTML5 selfclosing tags 
    $selfclosingTags = array('area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'); 

    /** 
     * Find out how many arguments we have and 
     * initialize the needed variables 
     */ 
    switch (func_num_args()) { 
        case 1: 
            $replace_with_space = true; 
            break; 
        case 2: 
            $arg = func_get_arg(1); 
            if ($arg === 1 || $arg === true || $arg === '1' || $arg === 'true') { 
                // for full legacy support || $arg === 'false' should be included 
                $replace_with_space = ' '; 
                $allowed_tags = ''; 
            } elseif ($arg === 0 || $arg === false || $arg === '0' || $arg === 'false') { 
                // for full legacy support || $arg === 'false' should be removed 
                $replace_with_space = ''; 
                $allowed_tags = ''; 
            } else { 
                $replace_with_space = ' '; 
                $allowed_tags = $arg; 
            } 
            break; 
        case 3: 
            $replace_with_space = func_get_arg(1) ? ' ' : ''; 
            $allowed_tags = func_get_arg(2); 
            break; 
    } 

    if (strlen($allowed_tags)) { 
        // Allowed tags are set 
        $allowed_tags = str_replace(array(' />', '/>', '>'), '', $allowed_tags); 
        $allowed_tags = substr(str_replace('<', '|', $allowed_tags), 1); 

        // This is to delete the allowed selfclosing tags from the list 
        $tagArray = explode('|', $allowed_tags); 
        $selfclosingTags = array_diff($selfclosingTags, $tagArray); 
        unset ($tagArray); 
    } 

    // Let's get rid of the selfclosing tags first 
    if (count($selfclosingTags)) { 
        $string = preg_replace('/<(' . implode('|', $selfclosingTags) . ')\s?[^>]*?\/>/is', $replace_with_space, $string); 
    } 

    // And now the other tags 
    if (strlen($allowed_tags)) { 
        while (preg_match("/<(?!({$allowed_tags}))([a-z1-5]+)\s?[^>]*?>([^<]*?)<\/\\2>/is", $string)) 
            $string = preg_replace("/<(?!({$allowed_tags}))([a-z1-5]+)\s?[^>]*?>([^<]*?)<\/\\2>/is", '$3' . $replace_with_space, $string); 
    } 
    else { 
        // Absolutely no tags allowed 
        while (preg_match("/<([a-z1-5]+)\s?[^>]*?>([^<]*?)<\/\\1>/is", $string)) 
            $string = preg_replace("/<([a-z1-5]+)\s?[^>]*?>([^<]*?)<\/\\1>/is", '$2' . $replace_with_space, $string); 
    } 

    return $string; 
} 

/* vim: set expandtab: */ 
?>


Как этим пользоваться:
{$string|strip_tags} — удаляет все теги и заменяет их пробелами;
{$string|strip_tags:false} — удаляет все теги и не заменяет их пробелами;
{$string|strip_tags:''} — удаляет все теги, за исключением <b> и заменяет их пробелами;
{$string|strip_tags:false:''} strips all tags except b and br tags without replacing them with a space
avatar
— Ошибся кнопкой, но не суть.
avatar
Попробую с утреца. Спс.
avatar
Как можно сделать, img вырезались, а остальные оставались?
если вас не пугает перечислить все остальные теги — тогда можно.
  • PSNet
  • 0
Только зарегистрированные и авторизованные пользователи могут оставлять комментарии.