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