【问题标题】:Loop through buffer content with different value循环遍历具有不同值的缓冲区内容
【发布时间】:2018-06-21 10:50:47
【问题描述】:

需要帮助,

我正在使用插件和匹配的 iframe 标记获取缓冲区数据。

获取缓冲区值后,我正在检索 iframe src 并将其替换为空白 src。

当我循环 src 值和输出并使用 preg_replace 它不会根据循环替换值并替换为第一个 iframe 值...

这是我的代码

add_action('wp_footer', 'scheck_iframe_value');

function scheck_iframe_value() {

    $get_me_buffers = ob_get_clean();       
    $pattern = '@(.*)(<iframe(?:.*?)</iframe>)(.*)@m';
    ob_start();

    /* if (preg_match($pattern, $get_me_buffers, $get_me_buffers_return)) {  */
    if (preg_match_all($pattern, $get_me_buffers, $get_me_buffers_return, PREG_PATTERN_ORDER)) {            
        $d_new_body_plus = $get_me_buffers_return[0];
        $html = '';
        $sizeofarray = count($d_new_body_plus);         
        for ($i = 0; $i < count($d_new_body_plus); $i++) {
            preg_match('/src="([^"]+)"/', $d_new_body_plus[$i], $match);

            $src = $match[1];
            $content = str_replace($src, '', $d_new_body_plus[$i]);
            $html .= '<div class="wpretarget-iframe-block" style="background-color: lightgray;text-align: center;">'
                    . '<button style="margin: 5px;background-color: blue;color: white;" type="button" class="wpretarget-iframe-content-button-click" data-url=' . $src . ' data-type="iframe">Click to load content of Vimeo</button>'
                    . '<span style="display:none;">' . $content . '</span>'
                    . '</div>';
            $d_new_body_plus = $html;
        }
        echo preg_replace($pattern, $d_new_body_plus, $get_me_buffers);
    } else {
        echo $get_me_buffers;
    }
    ob_flush();
}

【问题讨论】:

  • 尝试将$d_new_body_plus = $get_me_buffers_return[0]; 替换为$d_new_body_plus = $get_me_buffers_return;。如果你的主逻辑没有错误,你的代码应该可以工作。
  • @Petya 感谢您的回复。如果我用它替换,那么我将如何让 iframe src 更改。警告:preg_match() 期望参数 2 是字符串,数组中给出

标签: php wordpress plugins buffer bufferedreader


【解决方案1】:

我使用 preg_replace_callback 函数解决了这个问题。代码是

function test_iframe_checker() {   
    $get_me_buffers = ob_get_clean();    
    $pattern = '@(.*)(<iframe(?:.*?)</iframe>)(.*)@m';
    ob_start();           
    if (preg_match_all($pattern, $get_me_buffers, $get_me_buffers_return, PREG_PATTERN_ORDER)) {             
        echo $source = preg_replace_callback($pattern, function($matches){
             preg_match('/src="([^"]+)"/', $matches[0], $match);
             $src = $match[1];
             $contents = str_replace($src, '', $matches[0]);
            return $html ='<div class="test-iframe-block" style="background-color: lightgray;text-align: center;">'
                    . '<button style="margin: 5px;background-color: blue;color: white;" type="button" class="test-iframe-content-button-click" data-url=' . $src . ' data-type="iframe">Click to load content Third Party Control</button>'
                    . '<span style="display:none;">' . $contents . '</span>'
                    . '</div>';
         }, $get_me_buffers);
    } else {
        echo $get_me_buffers;
    }
    ob_flush();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    相关资源
    最近更新 更多