将Instagram URL替换为嵌入

时间:2018-02-16 20:07:48

标签: php regex curl instagram embed

如何使用正则表达式从嵌入格式的文本中替换Instagram URL?

let arbitrary_header: AribitraryNumber = res.headers().get::<ArbitraryNumberHeader>().unwrap();

1 个答案:

答案 0 :(得分:0)

正则表达式用于匹配字符串,而不是替换或类似的东西。 如果您想将文本转换为嵌入链接,这是一种方法:

<?php
    $text = 'There is https://www.instagram.com/p/Bes0orNjOIa/?taken-by=kendalljenner and then there was https://www.instagram.com/p/BZMh9qgl8Kw/?taken-by=felix.gaebler';
    $words = explode(' ', $text);

    $output = array();  

    foreach ($words as $word) {

        if(preg_match('/^https:\/\/www\.instagram\.com\/p\/.{6,}\?/', $word)) {

            $code = explode("/", $word)[4];
            $word = '<iframe src="//www.instagram.com/p/$code/embed"></iframe>';

        } 

        array_push($output, $word);

    }

    echo implode(' ', $output);
?>

此处输出:https://i.imgur.com/bHWKF6D.png