I'm working with PHP and MySQL and am displaying an article stored in a database by echoing $Content. I would like to include another file just before the second paragraph, which begins with [p]In fact, you (I replaced the paragraph tags with brackets here.)
This doesn't work...
$BaseINC = '../../Geobop';
$Content = str_replace('<p>In fact, you', ''.require_once($BaseINC."/$MyPHP/inc/A/Test.php").'', $Content);
It displays the error Warning: require_once(../../Geobop/2B/inc/A/Test.php
In fact, you) [function.require-once]: failed to open stream: If I change A/Test.php) to A/Test.php;) (adding a semi-colon), it fails again.
If I strip out the string I'm trying to replace, it works to a point.
$BaseINC = '../../Geobop';
$Content = str_replace('<p>In fact, you', ''.require_once($BaseINC."/$MyPHP/inc/A/Test.php").'', $Content);
The weird thing is, it replaces [p]In fact, you with a numeral 1, with the text I inserted in my test file (Test.php) displaying at the top of the page.
Can anyone tell me what I'm doing wrong?
I should also mention that Test.php is physically located on one website (MySite1), while the article I'm previewing is on another site. However, all my websites include files stored on MySite1. I've included thousands of files; I just haven't figured out how to do it with a str_replace operation. Thanks.
I don't think you want to require the page, but rather want the contents in a string, right? In that case, you should use file_get_contents($filename)
instead. This returns a string, which can be used as an argument for str_replace
. require
returns 1 if there was no error. It does not return its contents(!); unless it is explicitly returned from that file using a return statement
.
Turning it into:
$BaseINC = '../../Geobop';
$Content = str_replace('<p>In fact, you', file_get_contents($BaseINC."/$MyPHP/inc/A/Test.php"), $Content);
Using require
or include
does not give you their contents in a string. Instead it runs the code inside of it. If that is PHP, it is executed. If it is HTML, it is outputted. This is why you saw the string appear at the top of your page.