简体   繁体   中英

multiple ob_start() and ob_get_clean()

I want to replace items from the text with some functions. For example I want to put slider from the WYSIWYG i write something like this {SLIDER_8879}, where SLIDER-is function type, 8879-is parameter (for example folder).

So for example:

<?php
    function xxx($fname, $value){
echo"<span style='color:orange;'>$fname|$value this is CAROUSEL</span> ";   
}

function callback ($match) {
    if ($match[2] === "SLIDER"){
        return "<span style='color:red'>This is a slider</span>";
    }else if($match[2] === "POLL"){
        return rand(8888888888, 8888888888888888888888);            
    }else if($match[2] === "CAROUSEL"){
        xxx($match[2], $match[4]);
    }else{
        echo"<span style='color:blue'>ESIM INCH!</span>";   
    }
}

$body = "{{SLIDER_fff}} sdfasdfas dfasd {{POLL_8879}} dsgasdgasdgds {{CAROUSEL_888}} sdffasd f asd f a {{SLIDER_8879-folder}} dfgdf g dsfsfgfg d  fdfgd {{BIBIN_folder}} sdfsdf";
$pattern = "/({{2})([A-Z]+)(_{1})([a-zA-Z0-9_\-]+)(}{2})/";
$body = preg_replace_callback($pattern, "callback", $body);

echo $body;
?>

I get this ordering:

CAROUSEL|888 this is CAROUSEL ESIM INCH!This is a slider sdfasdfas dfasd 123055895 dsgasdgasdgds sdffasd f asd fa This is a slider dfgdf g dsfsfgfg d fdfgd sdfsdf

Want to have right replaceing order:

This is a slider sdfasdfas dfasd 123055895 dsgasdgasdgds CAROUSEL|888 this is CAROUSEL sdffasd f asd fa This is a slider dfgdf g dsfsfgfg d fdfgd ESIM INCH! sdfsdf

The callback should return the replacement string.

Perhaps this updated code is what you are looking for:

<?php
function xxx($fname, $value){
    return "<span style='color:orange;'>$fname|$value this is CAROUSEL</span> ";
}

function callback ($match) {
    if ($match[2] === "SLIDER"){
        return "<span style='color:red'>This is a slider</span>";
    }else if($match[2] === "POLL"){
        return rand(8888888888, 8888888888888888888888);
    }else if($match[2] === "CAROUSEL"){
        return xxx($match[2], $match[4]);
    }else{
        return "<span style='color:blue'>ESIM INCH!</span>";
    }
}

$body = "{{SLIDER_fff}} sdfasdfas dfasd {{POLL_8879}} dsgasdgasdgds {{CAROUSEL_888}} sdffasd f asd f a {{SLIDER_8879-folder}} dfgdf g dsfsfgfg d  fdfgd {{BIBIN_folder}} sdfsdf";
$pattern = "/({{2})([A-Z]+)(_{1})([a-zA-Z0-9_\-]+)(}{2})/";
$body = preg_replace_callback($pattern, "callback", $body);

echo $body;
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM