循环浏览具有不同值的缓冲区内容

循环浏览具有不同值的缓冲区内容

本文介绍了循环浏览具有不同值的缓冲区内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要帮助,

我正在使用插件和匹配的iframe代码获取缓冲区数据.

I am getting buffer data using plugin and matching iframe tag.

获得缓冲区值后,我将检索iframe src并将其替换为空白src.

After getting buffer value, I am retrieving iframe src and replacing them with blank src.

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

When I loop the src values and output and using preg_replace it does not replace value according to loop and replace with the first iframe values...

这是我的代码

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();
}

推荐答案

我使用preg_replace_callback函数解决了此问题.代码为

I resolved this problem using preg_replace_callback function.And the code are

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();
}

这篇关于循环浏览具有不同值的缓冲区内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:58