fire1
fire1

Reputation: 82

call php function from HTML string

I'm facing a problem to call PHP function from html code and fill arguments of function. After that the HTML code is output with returned values of functions.

for an example:

somewhere In PHP file are defined functions

function html_field($type,$name,$value){ 
//some code here
return $out;
}
// or other example function
function boldme($text){
return "<b>$text</b>";
}

After that is generated html output in string with php functions inside (like tags)

HTML String:

$html = "<h1><label for="foo">{call~html_field("text","name","value")} </label><input type="text" name="foo" id="foo" /> </h1>"

OR

$html = "<h1><label for="foo">{call~boldme("text")} </label><input type="text" name="foo" id="foo" /> </h1>"

The solution should ends, like:

$html = "<h1><label for="foo"><input type="text" name="name" ...> </label><input type="text" name="foo" id="foo" /> </h1>"

OR

$html = "<h1><label for="foo"><b>text</b> </label><input type="text" name="foo" id="foo" /> </h1>"

It is required to filter this string...

NOTE: The string containing the collected html data from templates and themes, they are unknowable files with pure HTML inside.

I was using preg_replace_callback to create needed functionality, but all gone now, thanks to my boss.... !@#!

Upvotes: 0

Views: 1412

Answers (2)

NemoStein
NemoStein

Reputation: 2098

If you need to parse a string and call some function based on it, you can use the preg_replace_callback function.

Something like this should do the trick:

$html = "<p>{functionName('value1', 'value2')}</p>";

function contentParser($matches)
{   
    $function = $matches[1];
    $parameters = array_slice($matches, 2);

    return call_user_func_array($function, $parameters);
}

function functionName($valueA, $valueB)
{
    return "You called functionName with values " . $valueA . " and " . $valueB;
}

echo preg_replace_callback(
    "/\{(\w+)\([\"']([^\"']+)[\"'](?:, ?[\"']([^\"']+)[\"'])?\)\}/",
    "contentParser",
    $html);

This will print the following:

You called functionName with values value1 and value2

Note that my regex have a big problem.
You can enclose the values (in your html) in single or double quotes (" or '), and you CAN mix them... This leads to a second problem, which you can't use either in your values (I don't check for escaped sequences).

A simpler patter that uses only one character as a value wrapper (and you can change that character, of course) is the following:

"/\{(\w+)\(#([^#]+)#(?:, ?#([^#]+)#)?\)\}/"

Here I'm using the sharp (#) as value delimiter, then, your html must look like this:

<p>{functionName(#value1#, #value2#)}</p>

Upvotes: 1

Emii Khaos
Emii Khaos

Reputation: 10085

Where do the $html Strings come from? If it's static code, use standard php:

$html = '<h1><label for="foo">' . html_field("text","name","value") . '</label><input type="text" name="foo" id="foo" /> </h1>';

If they are loaded from database or file or whatever, you have to options:

  • make your own template engine, much work, few bugs, time wasted
  • use a lightweight template engine like twig and define your functions as filters

Upvotes: 1

Related Questions