Last active
August 4, 2018 06:44
-
-
Save gskema/9e30ba912e1222be48d2cd8dbd0fc575 to your computer and use it in GitHub Desktop.
HTML whitespace trim RegEx (regular expression) for PHP. Useful for trimming HTML content before TCPDF processing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Trims whitespaces after any tags. Assumes that a tag ends with '/>', '\w>', '">'. | |
$html = preg_replace('#(["|\/|\w]>)(\s+)#', '$1', $html); | |
// Trims whitespaces before any tags. Assumes that tags start with '<\w+' | |
$html = preg_replace('#(\s+)(<\/?\w+)#', '$2', $html); | |
// Trims spaces between tags (both opening and closing). | |
// Assumes that a tag ends with '/>', '\w>', '">' and starts with '<\w+' | |
$html = preg_replace('#(["|\/|\w]>)(\s+)(<\/?\w+)#', '$1$3', $html); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment