使用preg_match和str_replace替换字符串变量中多个iframe的source属性

I have the following code to replace youtube iframe with my custom string output:

preg_match('/iframe src="([^"]+)"/', $value, $match);
$url = str_replace('https://www.youtube.com/embed/','',$match[1]);
$url = str_replace('?rel=0&enablejsapi=1','',$url);
return '<img src="https://img.youtube.com/vi/' . $url . '/mqdefault.jpg"/>';

But how do I implement the following code to some string which have multiple iframe but wrapped with other things that should stay when it returned:

$value = '<p style="text-align: center;"><iframe src="//www.youtube.com/embed/YZvJZrfw5oo" width="560" height="314" allowfullscreen="allowfullscreen"></iframe></p><p style="text-align: center;">Aut debitis debitis eius id recusandae. Eius unde beatae dicta neque numquam quod cupiditate. Vel dolor eos quia illo. Recusandae architecto aut quas nostrum earum totam exercitationem. Alias sint mollitia eaque molestias doloribus quia. Facere porro atque voluptatibus animi. Omnis nisi deleniti nisi sit rerum ratione rerum. Quis sed sit eveniet rerum repudiandae.<iframe src="//www.youtube.com/embed/yPg0Keqif0I" width="560" height="314" allowfullscreen="allowfullscreen"></iframe></p>';

//Which is the same as:

<p style="text-align: center;">
    <iframe src="//www.youtube.com/embed/YZvJZrfw5oo" width="560" height="314" allowfullscreen="allowfullscreen">
    </iframe>
</p>
<p style="text-align: center;">
    Aut debitis debitis eius id recusandae. Eius unde beatae dicta neque numquam quod cupiditate. Vel dolor eos quia illo. Recusandae architecto aut quas nostrum earum totam exercitationem. Alias sint mollitia eaque molestias doloribus quia. Facere porro atque voluptatibus animi. Omnis nisi deleniti nisi sit rerum ratione rerum. Quis sed sit eveniet rerum repudiandae.
    <iframe src="//www.youtube.com/embed/yPg0Keqif0I" width="560" height="314" allowfullscreen="allowfullscreen">
    </iframe>
</p>

Instead of using preg_match, which only catches one match, you can use preg_match_all with a slightly different regular expression:

$value = '<p style="text-align: center;"><iframe src="//www.youtube.com/embed/YZvJZrfw5oo" width="560" height="314" allowfullscreen="allowfullscreen"></iframe></p><p style="text-align: center;">Aut debitis debitis eius id recusandae. Eius unde beatae dicta neque numquam quod cupiditate. Vel dolor eos quia illo. Recusandae architecto aut quas nostrum earum totam exercitationem. Alias sint mollitia eaque molestias doloribus quia. Facere porro atque voluptatibus animi. Omnis nisi deleniti nisi sit rerum ratione rerum. Quis sed sit eveniet rerum repudiandae.<iframe src="//www.youtube.com/embed/yPg0Keqif0I" width="560" height="314" allowfullscreen="allowfullscreen"></iframe></p>';

preg_match_all('/src="(?:https?:)?\/\/www\.youtube\.com\/embed\/([^?"]+)(?:\?[^"]+)?"/', $value, $matches);

First of all I want to highlight the different regex: Instead of just capturing the whole source attribute, it includes the regex group ([^?"]+) and specifies the youtube url based on the strings you replaced in str_replace. Like this you can omit using str_replace. The group will contian only the necessary part that you need.

preg_match_all will save all matches of a specific group in an inner array with the groups ID as index. The value of $matches will be:

$matches = [
    [//Group 0 contains the whole match. This is what you got as well
        'src="//www.youtube.com/embed/YZvJZrfw5oo"',
        'src="//www.youtube.com/embed/yPg0Keqif0I"'
    ],
    [//This is the first group, that only contains the string that you need.
        'YZvJZrfw5oo',
        'yPg0Keqif0I'
    ]
];

By iterating over group 1 with array_map(func..., $matches[1]); you can address each video id seperately and create image tags with them.

$imageTags = array_map(function($videoId){
    return '<img src="https://img.youtube.com/vi/' . $videoId . '/mqdefault.jpg"/>';
}, $matches[1]);

The result will be an array of image tags. If you need to return a string instead an array you can combine the results with implode.

return implode('', $imageTags);