Asked  7 Months ago    Answers:  5   Viewed   24 times

Possible Duplicate:
Execute a PHP file, and return the result as a string
PHP capture print/require output in variable

I am trying to get the contents of an include to a string. Is that possible?

For example, if I have a test.php file:

echo 'a is equal to '.$a;

I need a function, say include_to_string to include the test.php and return what would be output by in in a string.

Something like:

$a = 4;
$string = include_to_string(test.php); // $string = "a is equal to 4"


include 'test.php';
$string = ob_get_clean();

I think is what you want. See output buffering.

Wednesday, March 31, 2021
answered 7 Months ago

If you need to determine the base path of a set of scripts, you should not rely on the "current working directory." This can change from executing environment to executing environment.

Instead, base it on a known path.

/includes/class_bootstrap.php knows that it's going to be one directory down from where the base path is going to be, so it can do this:

define('CWD', realpath(dirname(__FILE__) . '/../') );

dirname gets the directory name given in the passed string. If __FILE__ returns C:/wamp/www/vb4/plugins/includes/class_bootstrap.php, then dirname will return C:/wamp/www/vb4/plugins/includes. We then append /../ to it and then call realpath, which turns that relative .. into a real directory: C:/wamp/www/vb4/plugins


From that point forward, CWD will operate as you expect. You can require_once CWD . '/includes/init.php' and it will correctly resolve to C:/wamp/www/vb4/plugins/includes/init.php

Also, this may sound stupid but "vb4" may be referring to vBulletin 4, in which case your plugin may already have access to the configuration information that it exposes, including handy things like paths. This may make this entire exercise unnecessary. I intentionally know nothing about vB, otherwise I would point you at their dev docs.

Wednesday, March 31, 2021
answered 7 Months ago
>>> import re
>>> string1 = "498results should get"
>>> int('d+', string1).group())

If there are multiple integers in the string:

>>> map(int, re.findall(r'd+', string1))
Wednesday, June 9, 2021
answered 5 Months ago

Use String.valueOf():

int sdRate=5;
//text_Rate is a TextView
text_Rate.setText(String.valueOf(sdRate)); //no more errors
Wednesday, June 30, 2021
answered 4 Months ago

The includes are evaluated from the location of the running script. When you include another file, you are essentially pulling the contents of that file into the running script at that place.

For files that should evaluate includes relative to the included file's location, you can do this:


include(dirname(__FILE__) . '/';
include(dirname(__FILE__) . '/../asdf/'

From the documentation:

__FILE__ is The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.

dirname Given a string containing the path of a file or directory, this function will return the parent directory's path.

[] []

Wednesday, August 25, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :