0

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.... !@#!

6
  • It is unsafe, but you're looking for eval() Commented Apr 24, 2013 at 11:28
  • 4
    I'd actually prefer preg_replace_callback() over eval() any day. Commented Apr 24, 2013 at 11:29
  • Im trying to skip eval()... Commented Apr 24, 2013 at 11:29
  • 1
    Sorry if I misunderstand you, but, simply calling <?php echo myFunc($value); ?> isn't enough? Commented Apr 24, 2013 at 11:31
  • Extending NemoStein solution $HTML = 'your html '. myFunc($value). 'more html '; should seems to solve your issue. Why you need callback or Eval for that matter. Commented Apr 24, 2013 at 11:47

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.