PHP - include file inside an echo statement?

Hi,
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; ?>
</div>

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

include_once('include_contact_form.php');
include_once('include_display_google_map.php')

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

Thanks in advance.
sabecsAsked:
Who is Participating?
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.
0
DzynitCommented:
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.
0
DzynitCommented:
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('http://www.yoursite.com/pathtofile/include_contact_form.php');
$googlemap = file_get_contents('http://www.yoursite.com/pathtofile/include_display_google_map.php');
$content = str_replace( 'contact_form_here', $contact, $content);
$content = str_replace( 'google_map_here', $googlemap, $content);
echo $content;

Open in new window

0

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.
0
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
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.