3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php /** * Strip the HTML of Word documents * * @author rene.veldink * @author christoph.roensch */ class Hgs_Filter_StripWordHtml #implements Zend_Filter_Interface { /** * @var string */ const ALLOW_DEFAULT = '<b><i><sup><sub><em><strong><u><br><ol><li><ul><span><div><h1><h2><h3><p>'; /** * @var string */ protected $_allow = self::ALLOW_DEFAULT; /** * Constructor * * @param array|string $options * @return void */ public function __construct($options = array()) { if (is_string($options)) { $this->_allow = $options; } if (isset($options['allow'])) { $this->_allow = $options['allow']; } } /** * @param string $value * @return string */ public function filter($value) { return $this->strip_word_html($value, $this->_allow); } /** * - moved $allow default value to class property * - fixed regex for 'simplify style tags', <br> were replaced with <b> * - fixed bad escape sequence near $num_matches * @link http://php.net/manual/de/function.strip-tags.php#99643 */ private function strip_word_html($text, $allowed_tags = self::ALLOW_DEFAULT) { echo "Debug: $allowed_tags\n"; mb_regex_encoding('UTF-8'); //replace MS special characters first $search = array('/&lsquo;/u', '/&rsquo;/u', '/&ldquo;/u', '/&rdquo;/u', '/&mdash;/u'); $replace = array('\'', '\'', '"', '"', '-'); $text = preg_replace($search, $replace, $text); //make sure _all_ html entities are converted to the plain ascii equivalents - it appears //in some MS headers, some html entities are encoded and some aren't $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8'); //try to strip out any C style comments first, since these, embedded in html comments, seem to //prevent strip_tags from removing html comments (MS Word introduced combination) if(mb_stripos($text, '/*') !== FALSE){ $text = mb_eregi_replace('#/\*.*?\*/#s', '', $text, 'm'); } //introduce a space into any arithmetic expressions that could be caught by strip_tags so that they won't be //'<1' becomes '< 1'(note: somewhat application specific) $text = preg_replace(array('/<([0-9]+)/'), array('< $1'), $text); $text = strip_tags($text, $allowed_tags); //eliminate extraneous whitespace from start and end of line, or anywhere there are two or more spaces, convert it to one $text = preg_replace(array('/^\s\s+/', '/\s\s+$/', '/\s\s+/u'), array('', '', ' '), $text); //strip out inline css and simplify style tags $search = array('#<(strong|b)\s+[^>]*>(.*?)</(strong|b)>#isu', '#<(em|i)[^>]*>(.*?)</(em|i)>#isu', '#<u[^>]*>(.*?)</u>#isu'); $replace = array('<b>$2</b>', '<i>$2</i>', '<u>$1</u>'); $text = preg_replace($search, $replace, $text); //on some of the ?newer MS Word exports, where you get conditionals of the form 'if gte mso 9', etc., it appears //that whatever is in one of the html comments prevents strip_tags from eradicating the html comment that contains //some MS Style Definitions - this last bit gets rid of any leftover comments $num_matches = preg_match_all('/\<!--/u', $text, $matches); if ($num_matches) { $text = preg_replace('/\<!--(.)*--\>/isu', '', $text); } return $text; } } $a = "<p>Hey<br /><b>Ho</b>,<i>Lets Go!</i></p><ol><li>1</li></ol><ul><li>2</li></ul>"; $filter = new Hgs_Filter_StripWordHtml(Hgs_Filter_StripWordHtml::ALLOW_DEFAULT); $b = $filter->filter($a); var_dump($a == $b); echo "\n$b";

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Note: hhvm uses a JIT which only kicks in after a few runs. Since we run scripts only once, hhvm always runs slower.

VersionSystem time (s)User time (s)Memory (MiB)
7.1.70.0050.00317.55
7.1.60.0180.00719.46
7.1.50.0140.00717.26
7.1.00.0030.07722.35
7.0.200.0000.00716.72
7.0.140.0030.07022.00
7.0.70.0030.08321.68
7.0.60.0000.06021.91
7.0.50.0100.03322.10
7.0.40.0000.04720.18
7.0.30.0000.04320.16
7.0.20.0030.04320.16
7.0.10.0000.04320.13
7.0.00.0030.04020.14
5.6.280.0100.06720.88
5.6.220.0000.06320.54
5.6.210.0070.07720.64
5.6.200.0070.05720.98
5.6.190.0070.03721.07
5.6.180.0000.04721.19
5.6.170.0000.04320.96
5.6.160.0030.04020.98
5.6.150.0030.04021.05
5.6.140.0100.08021.09
5.6.130.0070.08021.08
5.6.120.0030.04021.04
5.6.110.0000.04321.04
5.6.100.0000.04021.03
5.6.90.0000.06321.02
5.6.80.0000.04020.34
5.6.70.0030.03020.57
5.6.60.0000.03320.42
5.6.50.0000.03720.36
5.6.40.0070.02720.46
5.6.30.0000.04020.52
5.6.20.0070.03320.41
5.6.10.0070.04020.32
5.6.00.0030.03720.38
5.5.360.0030.06020.50
5.5.350.0130.07020.41
5.5.340.0070.06720.93
5.5.330.0030.04020.85
5.5.320.0000.04320.85
5.5.310.0000.04320.89
5.5.300.0030.03720.85
5.5.290.0030.06320.95
5.5.280.0000.03720.81
5.5.270.0130.02720.77
5.5.260.0070.03720.94
5.5.250.0030.03720.70
5.5.240.0030.03320.31
5.5.230.0000.03720.13
5.5.220.0030.03020.28
5.5.210.0000.03720.26
5.5.200.0030.06020.21
5.5.190.0000.04020.20
5.5.180.0000.04320.27
5.5.160.0030.03020.27
5.5.150.0000.04020.25
5.5.140.0000.04020.16
5.5.130.0100.03020.29
5.5.120.0030.04020.10
5.5.110.0030.03720.25
5.5.100.0070.04720.10
5.5.90.0030.04020.00
5.5.80.0070.04720.13
5.5.70.0070.05720.08
5.5.60.0100.07320.18
5.5.50.0130.06320.05
5.5.40.0270.06020.08
5.5.30.0000.06720.17
5.5.20.0000.08020.07
5.5.10.0030.05719.99
5.5.00.0030.08020.14
5.4.450.0030.03319.27
5.4.440.0030.03719.49
5.4.430.0000.04019.52
5.4.420.0030.03719.53
5.4.410.0030.04319.14
5.4.400.0000.06019.04
5.4.390.0000.04318.85
5.4.380.0000.03719.13
5.4.370.0000.03719.16
5.4.360.0070.06318.95
5.4.350.0070.05719.09
5.4.340.0030.03019.23
5.4.320.0000.04019.03
5.4.310.0000.03719.13
5.4.300.0000.03719.16
5.4.290.0070.03319.12
5.4.280.0070.03319.05
5.4.270.0000.03719.04
5.4.260.0000.03719.12
5.4.250.0070.03019.12
5.4.240.0000.06319.04
5.4.230.0030.06318.96
5.4.220.0030.07719.02
5.4.210.0070.08018.99
5.4.200.0100.07019.02
5.4.190.0070.07019.04
5.4.180.0070.07319.08
5.4.170.0130.07019.22
5.4.160.0070.07318.88
5.4.150.0070.07718.90
5.4.140.0100.09016.49
5.4.130.0070.04716.51
5.4.120.0100.07016.41
5.4.110.0030.04016.39
5.4.100.0000.05716.56
5.4.90.0000.05716.48
5.4.80.0100.04316.55
5.4.70.0030.06316.54
5.4.60.0000.07716.37
5.4.50.0030.04316.54
5.4.40.0000.08016.45
5.4.30.0070.05716.45
5.4.20.0030.05016.36
5.4.10.0030.03716.52
5.4.00.0000.07315.88
5.3.290.0000.04014.69
5.3.280.0030.04714.75
5.3.270.0000.05714.73
5.3.260.0000.07714.69
5.3.250.0070.07314.59
5.3.240.0030.07714.76
5.3.230.0000.08014.61
5.3.220.0030.07314.65
5.3.210.0030.05714.52
5.3.200.0000.08314.52
5.3.190.0030.08014.51
5.3.180.0030.06014.56
5.3.170.0030.07314.56
5.3.160.0030.08014.70
5.3.150.0030.08014.54
5.3.140.0100.06714.50
5.3.130.0100.04314.63
5.3.120.0000.08314.55
5.3.110.0070.05714.63
5.3.100.0070.07014.14
5.3.90.0070.07314.10
5.3.80.0030.06713.96
5.3.70.0000.03714.09
5.3.60.0030.06014.13
5.3.50.0000.03713.95
5.3.40.0070.04313.95
5.3.30.0000.07013.93
5.3.20.0000.07713.74
5.3.10.0030.07013.69
5.3.00.0030.06013.75
5.2.170.0000.03312.38
5.2.160.0030.06312.38
5.2.150.0200.05012.38
5.2.140.0070.06012.38
5.2.130.0070.04012.38
5.2.120.0000.05712.38
5.2.110.0070.05312.38
5.2.100.0030.04012.38
5.2.90.0070.05712.38
5.2.80.0030.06312.38
5.2.70.0000.06712.38
5.2.60.0030.06012.38
5.2.50.0030.06312.38
5.2.40.0070.06012.38
5.2.30.0070.06312.38
5.2.20.0070.04312.38
5.2.10.0100.05312.38
5.2.00.0030.05312.38
5.1.60.0030.05712.38
5.1.50.0000.04712.38
5.1.40.0030.03712.38
5.1.30.0030.05312.38
5.1.20.0070.05312.38
5.1.10.0000.04012.38
5.1.00.0030.04012.38
5.0.50.0030.04712.38
5.0.40.0030.04712.38
5.0.30.0000.06712.38
5.0.20.0000.04712.38
5.0.10.0000.04012.38
5.0.00.0070.06312.38
4.4.90.0000.03012.38
4.4.80.0000.02712.38
4.4.70.0000.02312.38
4.4.60.0000.03712.38
4.4.50.0030.03312.38
4.4.40.0000.04712.38
4.4.30.0000.02712.38
4.4.20.0000.03312.38
4.4.10.0000.02312.38
4.4.00.0000.03312.38
4.3.110.0030.02712.38
4.3.100.0000.02712.38
4.3.90.0030.03312.38
4.3.80.0000.03312.38
4.3.70.0000.03712.38
4.3.60.0030.02312.38
4.3.50.0000.04012.38
4.3.40.0000.05012.38
4.3.30.0000.03712.38
4.3.20.0000.03712.38
4.3.10.0000.03712.38
4.3.00.0030.03312.38