2

So what I've got is a page stored in a database that has some content then the PHP function load_users then some more content. What I need to do is search the string retrieved from the database for PHP:FunctionName(Different on everypage) and then replace it with the functions return.

Lets say in the database I have

    <h1>Online Users</h1>
    PHP:LoadOnline
    <p>Blah blah blah blah blah</p>

I want to be able to search for that PHP: and get the function name beside it and then execute it and display the results. Is this possible or am I gonna have to figure something else out?

1
  • I have this: echo str_replace('PHP:load_users',load_users(),$pageString); But that requires that I put in what function to look for and it for some reason runs the function above the other content Commented Oct 1, 2013 at 3:15

1 Answer 1

2

You could use preg_replace_callback.

function printSomeText()
    {
    echo 'text';
    }

$template = "
 <h1>Online Users</h1>
    PHP:printSomeText sdf
    <p>Blah blah blah blah blah</p>";

echo preg_replace_callback(
    '/PHP\:([^\s]*)/', function ($matches)
        {
        $function = $matches[1];
        ob_start();
        $function();
        $return = ob_get_contents();
        ob_end_clean();
        return $return;
        }, $template
);

This way would be 'lazy' enough.

Sign up to request clarification or add additional context in comments.

3 Comments

The problem with that is I have to specify exactly what function I'm looking for which makes things difficult because I want the person editing pages to just be able to type PHP:WhateverFunction and it work plus that still doesn't explain why the functions return displays before any of the other content even though its in the middle of the content
Please specify your functions for examples.
Figured out why it was displaying incorrectly so thats solved. Basically lets say one function returns hi and another returns hello I want the client to be able to type PHP:hi and it run the function that returns hi but if they type PHP:hello it run the function that returns hello

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.