首页 > 解决方案 > 将 Pregmatch 值存储在变量中

问题描述

我有下面的代码从 $string 中单独删除 URL。我希望 Match[0] 的这个值在动态生成的 Iframe URL 中得到回显。我得到 1 作为输出..

 <?php

    $string = '<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3887.094472437723!2d77.7214372148226!3d13.029655590817852!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x3bae1037f4ecfb4b%3A0xe7e06043fc59c9f2!2sConcorde%20Auriga!5e0!3m2!1sen!2sin!4v1575539767295!5m2!1sen!2sin" width="600" height="450" frameborder="0" style="border:0;" allowfullscreen=""></iframe>';

    preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $string, $match);

    echo "<pre>";
    print_r($match[0]); 
    echo "</pre>";

    ?>

我尝试了什么:

  <div id="locate-us"></div>
                <div class="decoration decoration-margins"></div>   

                ';  // end content
                preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $_POST['property_maps_url'], $match);
                print_r($match[0]);

                // continue in content
                $content_to_write_amp .=' <amp-iframe class="full-bottom" width="600" height="400" layout="responsive" sandbox="allow-scripts allow-same-origin allow-popups" frameborder="0" src="'.print_r($match[0]).'"></amp-iframe>

                </div>

标签: php

解决方案


你剥离的 url 是 $match[0] 的第 0 个元素,所以像这样打印它 $match[0][0]。

以下是您的 div 的代码。

            <div class="decoration decoration-margins"></div>   

            ';  // end content
            preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $_POST['property_maps_url'], $match);
            print_r($match[0]);

            // continue in content
            $content_to_write_amp .=' <amp-iframe class="full-bottom" width="600" height="400" layout="responsive" sandbox="allow-scripts allow-same-origin allow-popups" frameborder="0" src="'.$match[0][0]).'"></amp-iframe>

            </div>

推荐阅读