I'm replacing all ocurrences in a string to . And I'm doing:
1) I get video_id from youtube url.
preg_match('/embed\/([\w+\-+]+)[\"\?]/', $string,$video_id);
2) I remove iframe with amp-youtube adding the url video id.
$string = preg_replace( '/<iframe\s+.*?\s+src=(".*?").*?<\/iframe>/',
'<amp-youtube data-videoid="'.$video_id[1].'" width="480" height="270" layout="responsive"></amp-youtube>', (str_replace("https://www.youtube.com/embed/","", $string)));
That works fine for just one ocurrence.
But If I have more than one iframe... Ok I can do
preg_match_all('/embed\/([\w+\-+]+)[\"\?]/', $string,$video_id);
to get all video ids in the string.
But how can I loop to add each id to every amp-youtube data-videoid in a string??
Thanks!!