3v4l.org

run code in 300+ PHP versions simultaneously
<?php function truncate($text, $length = 100, $ending = '...', $exact = false, $considerHtml = true) { if ($considerHtml) { // if the plain text is shorter than the maximum length, return the whole text if (strlen(preg_replace('/<.*?>/', '', $text)) <= $length) { return $text; } // splits all html-tags to scanable lines preg_match_all('/(<.+?>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER); $total_length = strlen($ending); $open_tags = array(); $truncate = ''; foreach ($lines as $line_matchings) { // if there is any html-tag in this line, handle it and add it (uncounted) to the output if (!empty($line_matchings[1])) { // if it's an "empty element" with or without xhtml-conform closing slash if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) { // do nothing // if tag is a closing tag } else if (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) { // delete tag from $open_tags list $pos = array_search($tag_matchings[1], $open_tags); if ($pos !== false) { unset($open_tags[$pos]); } // if tag is an opening tag } else if (preg_match('/^<\s*([^\s>!]+).*?>$/s', $line_matchings[1], $tag_matchings)) { // add tag to the beginning of $open_tags list array_unshift($open_tags, strtolower($tag_matchings[1])); } // add html-tag to $truncate'd text $truncate .= $line_matchings[1]; } // calculate the length of the plain text part of the line; handle entities as one character $content_length = strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2])); if ($total_length+$content_length> $length) { // the number of characters which are left $left = $length - $total_length; $entities_length = 0; // search for html entities if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) { // calculate the real length of all entities in the legal range foreach ($entities[0] as $entity) { if ($entity[1]+1-$entities_length <= $left) { $left--; $entities_length += strlen($entity[0]); } else { // no more characters left break; } } } $truncate .= substr($line_matchings[2], 0, $left+$entities_length); // maximum lenght is reached, so get off the loop break; } else { $truncate .= $line_matchings[2]; $total_length += $content_length; } // if the maximum length is reached, get off the loop if($total_length>= $length) { break; } } } else { if (strlen($text) <= $length) { return $text; } else { $truncate = substr($text, 0, $length - strlen($ending)); } } // if the words shouldn't be cut in the middle... if (!$exact) { // ...search the last occurance of a space... $spacepos = strrpos($truncate, ' '); if (isset($spacepos)) { // ...and cut the text in this position $truncate = substr($truncate, 0, $spacepos); } } // add the defined ending to the text $truncate .= $ending; if($considerHtml) { // close all unclosed html-tags foreach ($open_tags as $tag) { $truncate .= '</' . $tag . '>'; } } return $truncate; } truncate('<b>&lt;Hello&gt;</b> <img src="world.png" alt="" /> world!', 10); print("\n"); truncate('<table><tr><td>Heck, </td><td>throw</td></tr><tr><td>in a</td><td>table</td></tr></table>', 10); print("\n"); truncate("<em><b>Hello</b>&#20;w\xC3\xB8rld!</em>", 10); print("\n");

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).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0060.01018.40
8.3.50.0120.00322.05
8.3.40.0090.00619.08
8.3.30.0080.00819.26
8.3.20.0000.00820.19
8.3.10.0040.00423.45
8.3.00.0040.00419.68
8.2.180.0100.01317.00
8.2.170.0150.00022.96
8.2.160.0070.00720.47
8.2.150.0080.00024.18
8.2.140.0080.00024.66
8.2.130.0070.00326.16
8.2.120.0040.00420.98
8.2.110.0060.00321.23
8.2.100.0100.00318.16
8.2.90.0000.00818.13
8.2.80.0080.00017.97
8.2.70.0060.00318.18
8.2.60.0090.00018.22
8.2.50.0040.00419.73
8.2.40.0030.00518.28
8.2.30.0030.00518.51
8.2.20.0040.00418.00
8.2.10.0000.00818.25
8.2.00.0080.00017.96
8.1.280.0070.01425.92
8.1.270.0060.00322.27
8.1.260.0080.00026.35
8.1.250.0090.00028.09
8.1.240.0070.00717.63
8.1.230.0120.00018.02
8.1.220.0060.00317.91
8.1.210.0100.00018.77
8.1.200.0100.00317.48
8.1.190.0000.00817.60
8.1.180.0040.00418.10
8.1.170.0040.00418.84
8.1.160.0060.00322.01
8.1.150.0030.00518.83
8.1.140.0050.00317.66
8.1.130.0050.00317.87
8.1.120.0000.00717.61
8.1.110.0040.00417.67
8.1.100.0080.00017.68
8.1.90.0040.00417.71
8.1.80.0040.00417.71
8.1.70.0030.00517.59
8.1.60.0060.00317.66
8.1.50.0040.00417.73
8.1.40.0040.00417.68
8.1.30.0000.00817.88
8.1.20.0080.00017.76
8.1.10.0030.00517.69
8.1.00.0030.00617.75
8.0.300.0040.00418.77
8.0.290.0070.00317.00
8.0.280.0040.00418.42
8.0.270.0040.00417.38
8.0.260.0030.00517.36
8.0.250.0030.00317.24
8.0.240.0040.00417.20
8.0.230.0040.00317.17
8.0.220.0000.00717.06
8.0.210.0040.00417.18
8.0.200.0070.00017.13
8.0.190.0000.00817.08
8.0.180.0000.00917.08
8.0.170.0030.00617.14
8.0.160.0050.00217.19
8.0.150.0040.00417.01
8.0.140.0030.00617.15
8.0.130.0000.00613.61
8.0.120.0030.00517.10
8.0.110.0000.00917.16
8.0.100.0080.00016.98
8.0.90.0030.00517.20
8.0.80.0120.00417.14
8.0.70.0040.00417.28
8.0.60.0030.00617.06
8.0.50.0040.00417.20
8.0.30.0100.01217.40
8.0.20.0070.01217.62
8.0.10.0040.00417.09
8.0.00.0080.01017.09
7.4.330.0050.00015.09
7.4.320.0030.00316.82
7.4.300.0030.00316.70
7.4.290.0040.00416.88
7.4.280.0060.00316.91
7.4.270.0030.00416.85
7.4.260.0080.00016.71
7.4.250.0000.00716.85
7.4.240.0080.00016.79
7.4.230.0000.00716.76
7.4.220.0080.01116.79
7.4.210.0110.00716.75
7.4.200.0000.00716.83
7.4.160.0100.00716.82
7.4.150.0100.01017.40
7.4.140.0100.00917.86
7.4.130.0090.00916.75
7.4.120.0080.01016.86
7.4.110.0130.00316.78
7.4.100.0100.00716.61
7.4.90.0060.01216.91
7.4.80.0160.00619.39
7.4.70.0040.01316.92
7.4.60.0120.00916.79
7.4.50.0040.00416.68
7.4.40.0030.01516.73
7.4.30.0100.00716.75
7.4.10.0110.00716.71
7.4.00.0070.01115.58
7.3.330.0060.00013.58
7.3.320.0060.00013.62
7.3.310.0030.00316.64
7.3.300.0030.00616.61
7.3.290.0090.00916.61
7.3.280.0100.00916.66
7.3.270.0130.00317.40
7.3.260.0100.01416.58
7.3.250.0140.00516.55
7.3.240.0130.00416.81
7.3.230.0100.00616.84
7.3.210.0030.01616.59
7.3.200.0060.01619.39
7.3.190.0090.00916.69
7.3.180.0070.01316.85
7.3.170.0120.00616.63
7.3.160.0090.00616.68
7.3.130.0110.00716.80
7.3.120.0060.01115.89
7.3.110.0000.01616.59
7.3.100.0040.01416.45
7.3.90.0080.00816.47
7.3.80.0030.01416.53
7.3.70.0080.00416.62
7.3.60.0090.00016.67
7.3.50.0000.01616.63
7.3.40.0070.00316.52
7.3.30.0060.00916.64
7.3.20.0150.00318.35
7.3.10.0030.01318.29
7.3.00.0100.00318.36
7.2.330.0190.00016.91
7.2.320.0120.00616.94
7.2.310.0130.00317.05
7.2.300.0090.00917.03
7.2.290.0030.01217.04
7.2.260.0130.00716.91
7.2.250.0090.01216.86
7.2.240.0040.00817.02
7.2.230.0080.00816.98
7.2.220.0030.01316.91
7.2.210.0040.01416.69
7.2.200.0030.00916.87
7.2.190.0070.01016.91
7.2.180.0070.01016.68
7.2.170.0030.00716.86
7.2.160.0030.01316.81
7.2.150.0100.00718.59
7.2.140.0120.00318.57
7.2.130.0070.00718.64
7.2.120.0080.00618.77
7.2.110.0130.00718.74
7.2.100.0070.01118.61
7.2.90.0070.01118.56
7.2.80.0080.00418.75
7.2.70.0030.01318.71
7.2.60.0000.01218.84
7.2.50.0060.00618.64
7.2.40.0080.01218.52
7.2.30.0060.00618.66
7.2.20.0120.00618.85
7.2.10.0060.00818.53
7.2.00.0070.00619.09
7.1.330.0150.00017.78
7.1.320.0070.00717.67
7.1.310.0070.00317.79
7.1.300.0030.00917.70
7.1.290.0090.00917.55
7.1.280.0030.00617.38
7.1.270.0140.00017.75
7.1.260.0100.00617.48
7.1.250.0040.01117.50
7.1.240.0040.01517.61
7.1.230.0120.00617.50
7.1.220.0140.00417.60
7.1.210.0090.00617.55
7.1.200.0070.00717.52
7.1.190.0120.00417.34
7.1.180.0000.01417.48
7.1.170.0070.00717.54
7.1.160.0000.01817.44
7.1.150.0060.00317.61
7.1.140.0040.01417.63
7.1.130.0030.00917.58
7.1.120.0080.00417.49
7.1.110.0040.01117.62
7.1.100.0050.00718.00
7.1.90.0100.01017.59
7.1.80.0030.01417.68
7.1.70.0070.00917.44
7.1.60.0090.01018.69
7.1.50.0050.01117.34
7.1.40.0030.01017.54
7.1.30.0030.00717.41
7.1.20.0000.01417.57
7.1.10.0030.01417.47
7.1.00.0020.04419.93
7.0.330.0080.00617.15
7.0.320.0070.00717.23
7.0.310.0060.00917.23
7.0.300.0150.00017.27
7.0.290.0030.01517.03
7.0.280.0070.00717.16
7.0.270.0040.01117.31
7.0.260.0040.00817.17
7.0.250.0100.00317.23
7.0.240.0090.00617.15
7.0.230.0130.00717.27
7.0.220.0040.01117.33
7.0.210.0040.00717.19
7.0.200.0060.00917.08
7.0.190.0100.00317.23
7.0.180.0060.00917.33
7.0.170.0030.01317.33
7.0.160.0030.01217.24
7.0.150.0090.00617.30
7.0.140.0080.03819.63
7.0.130.0030.01017.16
7.0.120.0090.00917.34
7.0.110.0070.00717.39
7.0.100.0070.01017.25
7.0.90.0060.00617.30
7.0.80.0040.01517.17
7.0.70.0060.00917.14
7.0.60.0130.04318.58
7.0.50.0050.02717.58
7.0.40.0070.02417.61
7.0.30.0230.03417.73
7.0.20.0170.03917.72
7.0.10.0030.05017.68
7.0.00.0070.02917.75
5.6.400.0070.00416.42
5.6.390.0060.01015.97
5.6.380.0040.01115.97
5.6.370.0060.00615.70
5.6.360.0000.01316.25
5.6.350.0030.00716.20
5.6.340.0080.00816.16
5.6.330.0030.01016.17
5.6.320.0080.00515.80
5.6.310.0100.00316.38
5.6.300.0070.01016.12
5.6.290.0100.00315.77
5.6.280.0150.00316.21
5.6.270.0100.00716.17
5.6.260.0050.00816.08
5.6.250.0100.00016.21
5.6.240.0070.01016.14
5.6.230.0060.00916.07
5.6.220.0070.01016.38
5.6.210.0100.03518.26
5.6.200.0070.03217.16
5.6.190.0100.04318.35
5.6.180.1580.02818.28
5.6.170.0220.03818.36
5.6.160.0040.02518.19
5.6.150.0060.04417.15
5.6.140.0080.04417.05
5.6.130.0050.04917.12
5.6.120.0120.04218.54
5.6.110.0070.05018.55
5.6.100.0090.03518.47
5.6.90.0070.02018.46
5.6.80.0100.04018.20
5.6.70.2070.02218.34
5.6.60.0000.00915.93
5.6.50.0090.00316.30
5.6.40.0090.00916.05
5.6.30.0140.00316.31
5.6.20.0040.01415.80
5.6.10.0000.01715.97
5.6.00.0040.01115.85
5.5.380.0100.00715.81
5.5.370.0040.00716.07
5.5.360.0070.01115.80
5.5.350.0040.04918.33
5.5.340.0080.04017.07
5.5.330.0080.04118.37
5.5.320.0220.03718.28
5.5.310.0100.03518.07
5.5.300.0070.02217.12
5.5.290.0070.02716.93
5.5.280.0030.02518.49
5.5.270.0100.04218.34
5.5.260.0090.02218.49
5.5.250.0080.02518.27
5.5.240.0150.03118.15
5.5.230.0060.01016.05
5.5.220.0000.01215.82
5.5.210.0030.00615.95
5.5.200.0030.01315.93
5.5.190.0030.01116.02
5.5.180.0040.00816.16
5.5.170.0090.00615.88
5.5.160.0090.00615.84
5.5.150.0070.00316.03
5.5.140.0070.01115.48
5.5.130.0030.00715.97
5.5.120.0070.00715.99
5.5.110.0080.00415.87
5.5.100.0080.00816.08
5.5.90.0000.01015.84
5.5.80.0090.00615.81
5.5.70.0000.01616.08
5.5.60.0030.01016.05
5.5.50.0040.01115.80
5.5.40.0090.00615.88
5.5.30.0030.01016.04
5.5.20.0080.00615.79
5.5.10.0030.01015.84
5.5.00.0000.01015.95
5.4.450.0180.02228.73
5.4.440.0110.03128.51
5.4.430.0200.02228.45
5.4.420.0320.02428.30
5.4.410.0250.02728.29
5.4.400.0120.02128.43
5.4.390.0150.01828.32
5.4.380.0100.03215.69
5.4.370.0050.03215.70
5.4.360.0080.03215.69
5.4.350.0080.03015.65
5.4.340.0100.02815.83
5.4.330.0110.00012.91
5.4.320.0060.02212.73
5.4.310.0070.02612.61
5.4.300.0040.02212.65
5.4.290.0070.02112.79
5.4.280.0020.02412.59
5.4.270.0060.02212.57
5.4.260.0040.02612.56
5.4.250.0050.02812.68
5.4.240.0060.02212.62
5.4.230.0050.02512.69
5.4.220.0020.02812.71
5.4.210.0030.02312.58
5.4.200.0020.02512.56
5.4.190.0060.01912.61
5.4.180.0070.01812.63
5.4.170.0070.02112.56
5.4.160.0090.01912.69
5.4.150.0010.02412.69
5.4.140.0050.02512.50
5.4.130.0070.02112.44
5.4.120.0060.02112.38
5.4.110.0040.02312.55
5.4.100.0060.02112.44
5.4.90.0070.02812.43
5.4.80.0050.02512.44
5.4.70.0050.02112.44
5.4.60.0040.02212.46
5.4.50.0040.02112.46
5.4.40.0050.02112.45
5.4.30.0070.01912.48
5.4.20.0040.02512.41
5.4.10.0080.02012.42
5.4.00.0040.02012.25
5.3.290.0060.02713.02
5.3.280.0050.02412.95
5.3.270.0040.02913.09
5.3.260.0070.02012.95
5.3.250.0030.02712.94
5.3.240.0060.02312.90
5.3.230.0050.02112.90
5.3.220.0050.02412.87
5.3.210.0070.02312.90
5.3.200.0050.02112.97
5.3.190.0070.02212.96
5.3.180.0090.01912.97
5.3.170.0030.02312.91
5.3.160.0060.02012.96
5.3.150.0080.02113.04
5.3.140.0040.02312.96
5.3.130.0050.02412.93
5.3.120.0020.02412.93
5.3.110.0070.02212.93
5.3.100.0060.02112.73
5.3.90.0060.02112.72
5.3.80.0040.02112.58
5.3.70.0040.02412.58
5.3.60.0070.01912.67
5.3.50.0070.02012.48
5.3.40.0040.02212.49
5.3.30.0040.02212.56
5.3.20.0050.02112.44
5.3.10.0040.02312.34
5.3.00.0060.02412.26
5.2.170.0060.0309.26
5.2.160.0050.0299.27
5.2.150.0070.0289.26
5.2.140.0060.0289.26
5.2.130.0050.0299.22
5.2.120.0050.0289.22
5.2.110.0050.0299.23
5.2.100.0060.0299.23
5.2.90.0050.0309.22
5.2.80.0030.0329.22
5.2.70.0050.0299.22
5.2.60.0070.0289.17
5.2.50.0030.0319.14
5.2.40.0050.0289.13
5.2.30.0030.0319.11
5.2.20.0040.0309.09
5.2.10.0060.0268.99
5.2.00.0050.0288.85
5.1.60.0060.0228.13
5.1.50.0040.0258.13
5.1.40.0030.0268.12
5.1.30.0040.0268.47
5.1.20.0040.0288.49
5.1.10.0080.0278.21
5.1.00.0030.0288.21
5.0.50.0070.0186.70
5.0.40.0040.0196.56
5.0.30.0020.0326.37
5.0.20.0040.0206.34
5.0.10.0050.0186.32
5.0.00.0030.0316.32
4.4.90.0020.0164.78
4.4.80.0020.0164.76
4.4.70.0020.0174.75
4.4.60.0020.0164.75
4.4.50.0040.0144.77
4.4.40.0020.0264.71
4.4.30.0050.0134.76
4.4.20.0050.0144.85
4.4.10.0020.0164.85
4.4.00.0000.0284.76
4.3.110.0050.0154.67
4.3.100.0040.0184.66
4.3.90.0030.0154.63
4.3.80.0040.0234.62
4.3.70.0000.0174.63
4.3.60.0030.0144.63
4.3.50.0030.0164.63
4.3.40.0030.0244.56
4.3.30.0020.0163.37
4.3.20.0030.0183.36
4.3.10.0030.0143.30
4.3.00.0170.01743.80

preferences:
56.63 ms | 401 KiB | 5 Q