3v4l.org

run code in 300+ PHP versions simultaneously
<?php $content = ' <div id="content-15628" class="postcontent"> <h2>Responsive Images in WordPress 4.4</h2><p>WordPress 4.4 will add native responsive image support by including <code>srcset</code> and <code>sizes</code> attributes to the image markup it generates. For background on this feature, read the <a href="https://make.wordpress.org/core/2015/09/30/responsive-images-merge-proposal/">merge proposal</a>.</p> <h2>How it works</h2> <p>WordPress automatically creates several sizes of each image uploaded to the media library. By including the available sizes of an image into a <code>srcset</code> attribute, browsers can now choose to download the most appropriate size and ignore the others—potentially saving bandwidth and speeding up page load times in the process.</p> <p>To help browsers select the best image from the source set list, we also include a default <code>sizes</code> attribute that is equivalent to <code>(max-width: {{image-width}}px) 100vw, {{image-width}}px</code>. While this default will work out of the box for a majority of sites, themes should customize the default <code>sizes</code> attribute as needed using the <code>wp_calculate_image_sizes</code> filter.</p> <p>Note that for compatibility with existing markup, neither <code>srcset</code> nor <code>sizes</code> are added or modified if they already exist in content HTML.</p> <p>For a full overview of how <code>srcset</code> and <code>sizes</code> works, I suggest reading <em><a href="http://alistapart.com/article/responsive-images-in-practice">Responsive Images in Practice</a></em>, by Eric Portis over at A List Apart.</p> <h2>New functions and hooks</h2> <p>To implement this feature, we&#8217;ve added the following new functions to WordPress:</p> <ul> <li><code>wp_get_attachment_image_srcset()</code> – Retrieves the value for an image attachment&#8217;s <code>srcset</code> attribute.</li> <li><code>wp_calculate_image_srcset()</code> – A helper function to calculate the image sources to include in a <code>srcset</code> attribute.</li> <li><code>wp_get_attachment_image_sizes()</code> – Creates a <code>sizes</code> attribute value for an image.</li> <li><code>wp_calculate_image_sizes()</code> – A helper function to create the <code>sizes</code> attribute for an image.</li> <li><code>wp_make_content_images_responsive()</code> – Filters <code>img</code> elements in post content to add <code>srcset</code> and <code>sizes</code> attributes. For more information about the use of a display filter, read <a href="https://make.wordpress.org/core/2015/09/26/responsive-images-feature-plugin-update-2/">this post</a>.</li> <li><code>wp_image_add_srcset_and_sizes()</code> – Adds <code>srcset</code> and <code>sizes</code> attributes to an existing <code>img</code> element. Used by <code>wp_make_content_images_responsive()</code>.</li> </ul> <img src="http://blah.none/wp-content/2014/06/1.jpg" width="500" height="1000" alt="" class="wp-image-123" /> <p>As a safeguard against adding very large images to <code>srcset</code> attributes, we&#8217;ve included a <code>max_srcset_image_width</code> filter, which allows themes to set a maximum image width for images include in source set lists. The default value is 1600px.</p> <h2>A new default image size</h2> <p>A new default intermediate size, <code>medium_large</code>, has been added to better take advantage of responsive image support. The new size is 768px wide by default, with no height limit, and can be used like any other size available in WordPress. As it is a standard size, it will only be generated when new images are uploaded or sizes are regenerated with third party plugins.</p> <p>The <code>medium_large</code> size is not included in the UI when selecting an image to insert in posts, nor are we including UI to change the image size from the media settings page. However, developers can modify the width of this new size using the <code>update_option()</code> function, similar to any other default image size.</p> <h2>Customizing responsive image markup</h2> <img src="http://blah.none/wp-content/2014/06/1.jpg" width="300" height="400" alt="" class="wp-image-456" /> <p>To modify the default <code>srcset</code> and <code>sizes</code> attributes, you should use the <code>wp_calculate_image_srcset</code> and <code>wp_calculate_image_sizes</code> filters, respectively.</p> <p>Overriding the <code>srcset</code> or <code>sizes</code> attributes for images not embedded in post content (e.g. post thumbnails, galleries, etc.), can be accomplished using the <code>wp_get_attachment_image_attributes</code> filter, similar to how other image attributes are modified.</p> <p>Additionally, you can create your own custom markup patterns by using <code>wp_get_attachment_image_srcset()</code> directly in your templates. Here is an example of how you could use this function to build an <code>&lt;img&gt;</code> element with a custom <code>sizes</code> attribute:</p> <pre>&lt;?php $img_src = wp_get_attachment_image_url( $attachment_id, \'medium\' ); $img_srcset = wp_get_attachment_image_srcset( $attachment_id, \'medium\' ); ?&gt; &lt;img src="&lt;?php echo esc_url( $img_src ); ?&gt;" srcset="&lt;?php echo esc_attr( $img_srcset ); ?&gt;" sizes="(max-width: 50em) 87vw, 680px" alt="A rad wolf"&gt;</pre> <h2>Final notes</h2> <p>Users of the RICG Responsive Images Plugin should upgrade to <a href="https://wordpress.org/plugins/ricg-responsive-images/">version 3.0.0</a> now in order to be compatible with the functionality that included in WordPress 4.4.</p> '; $t = _microtime_float(); for ( $i = 0; $i < 1000; $i++ ) { $dom = new DOMDocument( '1.0' ); $dom->loadHTML( $content ); // \DOMNodesList $nodes = $dom->getElementsByTagName( 'img' ); if ( $nodes->length ) { $img = $nodes->item(0); $src = $img->getAttribute( 'src' ); $width = $img->getAttribute( 'width' ); $height = $img->getAttribute( 'height' ); $class = $img->getAttribute( 'class' ); } }

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.0110.00416.50
8.3.50.0110.00721.90
8.3.40.0040.01118.57
8.3.30.0120.00318.61
8.3.20.0060.00320.34
8.3.10.0070.00021.81
8.3.00.0060.00319.38
8.2.180.0000.01518.43
8.2.170.0040.01422.96
8.2.160.0140.00420.38
8.2.150.0050.00324.18
8.2.140.0040.00424.66
8.2.130.0060.00320.26
8.2.120.0040.00426.35
8.2.110.0040.00422.12
8.2.100.0080.00317.78
8.2.90.0000.00818.09
8.2.80.0000.00818.09
8.2.70.0070.00319.25
8.2.60.0040.00417.63
8.2.50.0040.00418.07
8.2.40.0000.00819.48
8.2.30.0040.00420.52
8.2.20.0090.00017.69
8.2.10.0050.00318.12
8.2.00.0040.00418.08
8.1.280.0110.00325.92
8.1.270.0040.00423.79
8.1.260.0040.00428.09
8.1.250.0060.00328.09
8.1.240.0130.00919.00
8.1.230.0090.00320.66
8.1.220.0050.00317.74
8.1.210.0050.00318.77
8.1.200.0000.00817.35
8.1.190.0060.00317.22
8.1.180.0070.00018.10
8.1.170.0000.00818.51
8.1.160.0000.00721.98
8.1.150.0000.00718.71
8.1.140.0000.00819.52
8.1.130.0030.00317.67
8.1.120.0050.00217.41
8.1.110.0070.00017.40
8.1.100.0040.00417.41
8.1.90.0070.00017.38
8.1.80.0000.00717.37
8.1.70.0000.00717.34
8.1.60.0020.00517.49
8.1.50.0040.00417.60
8.1.40.0000.00817.41
8.1.30.0050.00317.74
8.1.20.0050.00317.62
8.1.10.0000.00817.61
8.1.00.0020.00517.56
8.0.300.0040.00419.89
8.0.290.0090.00016.63
8.0.280.0000.00718.31
8.0.270.0000.00717.20
8.0.260.0000.00617.10
8.0.250.0030.00317.03
8.0.240.0000.00716.96
8.0.230.0000.00717.01
8.0.220.0040.00416.95
8.0.210.0040.00416.90
8.0.200.0030.00316.83
8.0.190.0050.00216.84
8.0.180.0040.00416.84
8.0.170.0080.00016.92
8.0.160.0040.00416.93
8.0.150.0040.00416.95
8.0.140.0040.00416.87
8.0.130.0040.00413.37
8.0.120.0070.00316.91
8.0.110.0040.00416.78
8.0.100.0000.00716.92
8.0.90.0040.00417.03
8.0.80.0030.01016.93
8.0.70.0000.00816.86
8.0.60.0080.00016.89
8.0.50.0040.00416.85
8.0.30.0100.00916.94
8.0.20.0110.01017.40
8.0.10.0050.00217.06
8.0.00.0070.01016.72
7.4.330.0000.00516.71
7.4.320.0000.00616.48
7.4.300.0030.00316.62
7.4.290.0030.00316.53
7.4.280.0060.00316.52
7.4.270.0030.00316.62
7.4.260.0000.00716.59
7.4.250.0050.00416.42
7.4.240.0020.00516.59
7.4.230.0030.00316.27
7.4.220.0130.00616.46
7.4.210.0110.00816.60
7.4.200.0000.00716.69
7.4.160.0040.01116.62
7.4.150.0070.01317.40
7.4.140.0130.00517.86
7.4.130.0090.00916.70
7.4.120.0080.01316.49
7.4.110.0030.01616.28
7.4.100.0090.00916.45
7.4.90.0090.00916.52
7.4.80.0100.01319.39
7.4.70.0120.00616.50
7.4.60.0070.01016.63
7.4.50.0120.00216.32
7.4.40.0100.00716.52
7.4.30.0110.00716.25
7.4.00.0060.00914.79
7.3.330.0070.00013.19
7.3.320.0050.00013.04
7.3.310.0000.00716.36
7.3.300.0000.00716.33
7.3.290.0000.00616.18
7.3.280.0070.00916.25
7.3.270.0090.00917.40
7.3.260.0090.00816.61
7.3.250.0110.01016.37
7.3.240.0100.01116.49
7.3.230.0070.01116.39
7.3.210.0060.01016.51
7.3.200.0100.00716.20
7.3.190.0080.00816.28
7.3.180.0180.00016.42
7.3.170.0120.00616.44
7.3.160.0060.00916.41
7.3.120.0100.00414.85
7.3.110.0100.00314.49
7.3.100.0100.00714.77
7.3.90.0050.00514.70
7.3.80.0080.00814.68
7.3.70.0060.01214.45
7.3.60.0030.01214.80
7.3.50.0030.00714.91
7.3.40.0060.00314.30
7.3.30.0060.00914.68
7.3.20.0080.00416.47
7.3.10.0000.01016.32
7.3.00.0030.01116.52
7.2.330.0100.00716.31
7.2.320.0060.01216.57
7.2.310.0040.01316.54
7.2.300.0120.01216.24
7.2.290.0070.01016.40
7.2.250.0110.00714.96
7.2.240.0060.00614.79
7.2.230.0120.00614.89
7.2.220.0000.00914.77
7.2.210.0070.00714.75
7.2.200.0030.01514.59
7.2.190.0000.01614.84
7.2.180.0060.00614.93
7.2.170.0070.00714.54
7.2.60.0030.01016.82
7.2.00.0000.01119.46
7.1.330.0030.00815.65
7.1.320.0070.00415.64
7.1.310.0000.01415.65
7.1.300.0030.00915.20
7.1.290.0040.00715.60
7.1.280.0000.01015.67
7.1.270.0040.00415.41
7.1.260.0070.00415.43
7.1.200.0030.00615.77
7.1.100.0030.00618.10
7.1.70.0110.00616.68
7.1.60.0100.01419.36
7.1.50.0040.00716.63
7.1.00.0100.07322.30
7.0.200.0040.00416.70
7.0.60.0100.04320.01
7.0.50.0070.08017.90
7.0.40.0100.07320.30
7.0.30.0200.04020.09
7.0.20.0330.05720.33
7.0.10.0070.04320.17
7.0.00.0070.05020.04
5.6.280.0070.06320.92
5.6.210.0000.05020.52
5.6.200.0030.04318.11
5.6.190.0200.07720.61
5.6.180.0330.04020.60
5.6.170.0230.04320.42
5.6.160.0100.08720.46
5.6.150.0100.07018.12
5.6.140.0130.06718.18
5.6.130.0200.07018.20
5.6.120.0000.05321.01
5.6.110.0030.09321.09
5.6.100.0100.05721.07
5.6.90.0130.08020.97
5.6.80.0130.06320.43
5.5.350.0130.08020.29
5.5.340.0130.07317.92
5.5.330.0100.07720.25
5.5.320.0200.06720.51
5.5.310.0200.04320.32
5.5.300.0070.04017.94
5.5.290.0030.08717.92
5.5.280.0070.03720.89
5.5.270.0100.08020.98
5.5.260.0030.08720.88
5.5.250.0100.08320.58
5.5.240.0170.08020.28

preferences:
57.18 ms | 401 KiB | 5 Q