Vinayak Mali
Vinayak Mali

Reputation: 1

How to change multiple <img> tag into <amp-img>?

<?php

$content_description = '<p><strong><em>They have added their own unique spin to marketing technology and convinced us that their refreshing understanding of the whys and wherefores of this rapidly growing industry is just one of the several ways to look at business. In this feature, we bring you the views of key executives who discuss how their ‘being’ pours into their professional personas&nbsp; </em></strong></p>

<p style="text-align: center;"><span style="color:#FF8C00;font-size:21px;"><strong>How does being a woman as a part of an exponentially explosive marketing tech space help you to be better equipped to understand this constantly evolving sector through your unique perspective?</strong></span></p>

<p><span style="color:#000080;"><strong>Nikki Nixon, Director #FlipMyFunnel at Terminus</strong></span></p>

<p><img alt="" class="lazy" src="https://images.martechadvisor.com/images/uploads/ckimages/images/nikki_nixon_terminus_58c049be4adc8.jpg" style="float: left; width: 125px; height: 107px; margin: 3px;" />“Being a woman in marketing tech, coupled with being an introvert, has given me tremendous advantages. I’ve had the opportunity to grow two software companies in the space and be uniquely empathetic along the way. My introversion allows me to think critically about the pros and cons of a given tool beyond the surface sales pitch. It allows me to dig deeper and ask questions to determine if the tool will really solve my needs.”</p>

<p><span style="color:#000080;"><strong>Jen Spencer, Vice President of Sales and Marketing at Allbound</strong></span></p>

<p><img alt="" class="lazy" src="https://images.martechadvisor.com/images/uploads/ckimages/images/jen_square_58c049dae10c2.jpg" style="float: right; width: 125px; height: 103px; margin: 3px;" />“Since I’m a woman, and I’ve always been a woman, I don’t know any other way of being. I can’t begin to assume I know what makes me as a woman better suited to lead a sales and marketing team at a SaaS company than a man – except maybe that having had twins, I know I have more stamina than most. Getting by on 4 hours of sleep a night? No problem! Getting those 4 hours of sleep in 1 hour increments over a 12-hour period? I. Will. Win.<br />
?>

I need to Replace all the Img tag into <amp-img> Format of all images should look like.<amp-img src="/img/amp.jpg" width="1080" height="610" layout="responsive" alt="AMP"></amp-img>

Upvotes: 0

Views: 2144

Answers (2)

PPL
PPL

Reputation: 6555

Use .replaceWith() API to replace the content :

$('p').find('img').replaceWith(function () {
   return '<amp-img src="'+this.src+'" width="1080" height="610" layout="responsive" alt="'+this.alt+'"></amp-img>'
 });

Using PHP Code:

$html = preg_replace('/<img(.*?)\/?>/', '<amp-img$1></amp-img>',$content_description);

Hope this works for you.

Upvotes: 2

Bachcha Singh
Bachcha Singh

Reputation: 3934

You can achieve your goal by using following approach

  1. Remove inline css for content, because amp does not support inline css
  2. Convert the <img> tag into <amp-img> tag, also add the height, width and layout (amp requirement)

Here is working Url : Click Here

Code :

<?php
$content_description = '<p><strong><em>They have added their own unique spin to marketing technology and convinced us that their refreshing understanding of the whys and wherefores of this rapidly growing industry is just one of the several ways to look at business. In this feature, we bring you the views of key executives who discuss how their ‘being’ pours into their professional personas&nbsp; </em></strong></p>

<p style="text-align: center;"><span style="color:#FF8C00;font-size:21px;"><strong>How does being a woman as a part of an exponentially explosive marketing tech space help you to be better equipped to understand this constantly evolving sector through your unique perspective?</strong></span></p>

<p><span style="color:#000080;"><strong>Nikki Nixon, Director #FlipMyFunnel at Terminus</strong></span></p>

<p><img alt="" class="lazy" src="https://images.martechadvisor.com/images/uploads/ckimages/images/nikki_nixon_terminus_58c049be4adc8.jpg" style="float: left; width: 125px; height: 107px; margin: 3px;" />“Being a woman in marketing tech, coupled with being an introvert, has given me tremendous advantages. I’ve had the opportunity to grow two software companies in the space and be uniquely empathetic along the way. My introversion allows me to think critically about the pros and cons of a given tool beyond the surface sales pitch. It allows me to dig deeper and ask questions to determine if the tool will really solve my needs.”</p>

<p><span style="color:#000080;"><strong>Jen Spencer, Vice President of Sales and Marketing at Allbound</strong></span></p>

<p><img alt="" class="lazy" src="https://images.martechadvisor.com/images/uploads/ckimages/images/jen_square_58c049dae10c2.jpg" style="float: right; width: 125px; height: 103px; margin: 3px;" />“Since I’m a woman, and I’ve always been a woman, I don’t know any other way of being. I can’t begin to assume I know what makes me as a woman better suited to lead a sales and marketing team at a SaaS company than a man – except maybe that having had twins, I know I have more stamina than most. Getting by on 4 hours of sleep a night? No problem! Getting those 4 hours of sleep in 1 hour increments over a 12-hour period? I. Will. Win.<br />';
$content = preg_replace('/style=[^>]*/', '', $content_description);
$html = preg_replace('/<img(.*?)\/?>/', '<amp-img$1 width="1080" height="610" layout="responsive" alt="AMP"></amp-img>',$content);
echo $html;
?>

Upvotes: 2

Related Questions