dongtangu8403 2018-01-15 12:50
浏览 140

使用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>
  • 写回答

1条回答 默认 最新

  • donglu4633 2018-01-15 13:40
    关注

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等