Clean HTML
What users paste is rarely what you want to store. VEditor turns office markup into plain HTML in the browser, and the PHP sanitizer checks the result against the same rules on save.
What happens when you paste
Word lists
mso-listparagraphs become nestedulandolwith the right numbering type and start number. Numbered headings become list items too.Google Docs
Lists nested directly in lists are repaired and
dir="ltr"noise is dropped.Tables from Word and Excel
Paragraphs in cells become line breaks,
tfootjoins the body, nested tables are flattened into the cell text andcolgroupis removed.Formatting from styles
Bold, italic, underline and strikethrough set with
<span style>becomestrong,em,uands.Dangerous addresses
javascript:,data:and every scheme other than http, https, mailto and tel are removed from links, images and media, also when hidden with control characters.Scripts and handlers
script,style,on*attributes and unknown elements never reach the content.Embeds
Only YouTube (no-cookie) and Vimeo iframes inside a figure, with
sandboxand a referrer policy.Your styles stay
Classes from your
stylesoption are allowed; random office classes and inline CSS outside the whitelist are not.Images from the clipboard
Image files are uploaded through your
uploadfunction instead of being embedded as base64. Images copied from Word together with text are left out.Spaces and empty blocks
In the saved HTML a single non-breaking space between words becomes a normal one. Empty paragraphs at the end, empty captions and the line breaks Google Docs puts between paragraphs are dropped.
One content model, two sanitizers
The editor’s schema in JavaScript and `HtmlSanitizer` in PHP implement the same algorithm and the same whitelist.
- Same rulesTags, attributes with their patterns, classes and CSS properties with allowed values.
- Byte-identical outputValid editor content passes the PHP sanitizer unchanged, so a difference means something was tampered with.
- The server decidesCleaning in the browser is a convenience.
HtmlSanitizeron the server is the security boundary, built onDOMDocumentwith no libraries. - Styles in one placeThe same
stylesarray feeds the editor’s “Style” list andallowStyles()in PHP. - Size and encoding
LengthExceptionabovemaxLength(1 MB by default), invalid UTF-8 cleaned before parsing. - Integrations included
sanitize()in the Laravel, Symfony and Yii2 integrations uses your styles automatically.
Sanitize on every save
One class, no configuration needed for the editor’s default content.
use VEditor\HtmlSanitizer;
$styles = [['name' => 'Lead', 'element' => 'p', 'classes' => ['lead']]];
try {
$clean = (new HtmlSanitizer())
->allowStyles($styles) // the same array as the editor's styles option
->sanitize((string) $_POST['content']);
} catch (LengthException $e) {
// larger than maxLength (1 MB by default)
}
Every option of the sanitizer and the editor schema is described in the documentation.
Try it in your browser
The demo has the full toolbar and shows what the PHP sanitizer returns for your content.