PHP - include file inside an echo statement?

I am trying to include a PHP file when a specific string is found in my content.

I retrieve my page content from my database for that page, then look for $content specific strings

$content = str_replace( "contact_form_here", "include_once('include_contact_form.php');", $content);
$content = str_replace( "google_map_here", "include_once('include_display_google_map.php')", $content);

<div id=”page_content”>
<?php echo $content; ?>

But when I echo the content to the page it just print the following output


Is there a way I can include above within an echo statement?

Thanks in advance.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
No, you can't do that.  'str_replace( )' is expecting a text variable, not a function, where you are trying to 'include' files.

You need to use a different function like 'file_get_contents' that will read that file into a variable.  That variable can then be used in your 'str_replace( )' function.
I think shortcode would work better in this case. If you're going to specifically type something like contact_form_here you would just do it like this [contact_form_here] and set up your function to do it when it sees that in the content.
I was thinking this was a wordpress question, so my shortcode suggestion will probably not work for you, but here's a quick code chunk that can do the str_replace you're looking for if you already have set the $content variable:

$contact = file_get_contents('');
$googlemap = file_get_contents('');
$content = str_replace( 'contact_form_here', $contact, $content);
$content = str_replace( 'google_map_here', $googlemap, $content);
echo $content;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sabecsAuthor Commented:
Thanks Dzynit, that is perfect and very much appreciated.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.