Warning: fopen(https://www.tutorialsplane.com/c.png): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in /home/u613989013/domains/tutorialsplane.com/public_html/wp-content/plugins/amp/includes/lib/class-fastimage.php on line 31

Warning: fopen(https://www.tutorialsplane.com/apa.png): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in /home/u613989013/domains/tutorialsplane.com/public_html/wp-content/plugins/amp/includes/lib/class-fastimage.php on line 31

Warning: fopen(https://www.tutorialsplane.com/([^): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in /home/u613989013/domains/tutorialsplane.com/public_html/wp-content/plugins/amp/includes/lib/class-fastimage.php on line 31
Php Replace Img tag by amp-img | AMP Image Tag in PHP Example
Tutorialsplane

Php Replace Img tag by amp-img


Php Replace Img tag by amp-img– We can use regular expression and str_replace to replace image tag by amp image tag.


Php Replace Img tag by amp-img | AMP image tag Example

You can use the below example to replace image tag by amp-img tag-

Php Replace Img tag by amp-img | Example:

<?php $content = "Hello Welcome to Tutorialsplane <img src='abc.png' alt='AMP Example' height='200'width='300' ?>. Learn more";
$result = preg_replace('/(]+>(?:<\/img>)?)/i', '$1', $content);
echo str_replace('

The above example will take care about height width and alt tag of amp-img so you do’nt need to worry about anything.

Example 2 Using preg_replace

echo htmlentities(preg_replace(
    '/<amp-img src="([^" width="600" height="400" class="amp-wp-unknown-size amp-wp-enforced-sizes" sizes="(min-width: 600px) 600px, 100vw"></amp-img>/', 
    '<amp-img src="$1" width="200" height="100" layout="responsive" alt="Amp Images"></amp-img>', 
    '<amp-img src="apa.png" width="600" height="400" class="amp-wp-unknown-size amp-wp-enforced-sizes" sizes="(min-width: 600px) 600px, 100vw"></amp-img><br/>hi bro <amp-img src="c.png" width="600" height="400" class="amp-wp-unknown-size amp-wp-enforced-sizes" sizes="(min-width: 600px) 600px, 100vw"></amp-img>'
));

Php - FAQ