最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Cannot get img src attribute to be allowedAttribute, it still strips it before putting it in the database - Stack Overflow

programmeradmin2浏览0评论

I am trying to get blog entries into a database with tags and attributes intact. Below, I configured HtmlSanitizer to allow certain elements and attributes. It seems to be working well, except that it strips the src attribute from images. I can't figure out what I am doing wrong:

require_once '../../project_core/vendor/autoload.php';

use Symfony\Component\HtmlSanitizer\HtmlSanitizer;
use Symfony\Component\HtmlSanitizer\HtmlSanitizerConfig;

// Allowed HTML elements and attributes
$allowedElements = array(
'div', 'p', 'br', 'strong', 'i', 'button', 'code', 'pre', 'em', 'a', 'ul', 'ol', 'li', 
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'img', 'table', 'tr', 'td', 'th', 'thead', 'tbody', 
'tfoot', 'hr', 'span'
);

$config = (new HtmlSanitizerConfig())
     ->allowSafeElements($allowedElements)
     ->allowRelativeLinks()
     ->allowAttribute('href', 'a')
     ->allowAttribute('title', '*')
     ->allowAttribute('target', 'a')
     ->allowAttribute('src', 'img')
     ->allowAttribute('alt', 'img')
     ->allowAttribute('style', '*')
     ->allowAttribute('class', '*')
     ->allowAttribute('id', '*')
     ->allowAttribute('width', 'img')
     ->allowAttribute('height', 'img')
     ->allowAttribute('border', 'img')
     ->allowAttribute('cellspacing', 'table')
     ->allowAttribute('cellpadding', 'table')
     ->allowAttribute('colspan', 'td')
     ->allowAttribute('rowspan', 'td')
     ->allowAttribute('scope', 'th')
     ->allowAttribute('align', 'table', 'td', 'th')
     ->allowAttribute('valign', 'td', 'th')
     ->allowAttribute('lang', '*')
     ->allowAttribute('dir', '*')
     ->allowAttribute('type', '*')   
     ->allowAttribute('value', '*')
     ->allowAttribute('name', '*')
     ->allowAttribute('placeholder', '*')
     ->allowAttribute('onclick', '*');

$postSanitizer = new HtmlSanitizer($config);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论