Quick actions

cmd+k|ctrl+k

Navigation

Languages

text2func | board code with function task

Snippet info

Language

Php

Visibility

public

Author

mertskaplan

Created

2022-08-25T19:58:35.883587Z

Updated

2022-08-25T20:06:24.866547Z

<?php

function text2func($text) { // board code with function task
    preg_match_all('/\[(.*)](.*)\[\/(.*)]/', $text, $outputs, PREG_SET_ORDER);
    foreach ($outputs as $out) {
        $values = explode(',', $out[2]);
        $text = str_replace('['. $out[1] .']'. $out[2] .'[/'. $out[3] .']', call_user_func_array($out[1],$values), $text);
    }
    return nl2br($text);
}

// Using

function exampleFunction ($value) {
    return "real";
}
function exampleFunction2 ($url, $alt, $width) {
    return "<img src='". $url ."' alt='$alt' width='$width'>";
}

$text = "Lorem Ipsum is simply [exampleFunction]dummy[/exampleFunction] text of the printing and typesetting industry. 
[exampleFunction2]/exampleImageURL.jpg,explanation text,200[/exampleFunction2]";

echo text2func($text);
INFO