Categories > Your tips & tricks >

Using built-in PHP functions with the onformat parameter

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Mick@el
Date: 2007-05-15
Time: 12:44

Using built-in PHP functions with the onformat parameter

If you want to use some PHP Functions (like stripslashes or str_replace) with the "onformat" field parameter, you can insert these functions in your PHP Code :
function PhpFunc($FieldName, &$CurrVal, &$CurrPrm)
{
    $fct = $CurrPrm['fct'];
   
    if (function_exists($fct))
    {
        $prms = Array();
        $i    = 1;
       
        while (isset($CurrPrm['prm' . $i]))
        {
            $prms[] = $CurrPrm['prm' . $i];
            $i++;
        }
       
        $CurrVal = call_user_func_array($fct, $prms);
    }
}

function SinglePhpFunc($FieldName, &$CurrVal, &$CurrPrm)
{
    $fct = $CurrPrm['fct'];

    if (function_exists($fct))
    {
        $CurrVal = $fct($CurrVal);
    }
}

Examples :

[myfield; onformat=SinglePhpFunc; fct=stripslashes]
[myfield; onformat=PhpFunc; fct=str_replace; prm1=Hello; prm2=Hi; prm3=[val]]
By: Don Bledsoe
Date: 2007-06-20
Time: 23:53

Re: Using built-in PHP functions with the onformat parameter

This is a wonderful idea I would never have thought of and works perfectly. Thanks!