Mr. B Mr. B -4 years ago 69
PHP Question

Define a PHP array() with the contents of an included file.

I'm trying to figure out how to define an array with the contents of another file. Is it possible? If yes, can you help point me in the right direction?

Current Array

array(
'
html code
',
'
html code
',
'
);


Desired Array

array(
include "arrays/htmlCode.php";
);


Contents of htmlCode.php file

'
html code
',
'
html code
',
'


Results of Code

include "arrays/htmlCode.php"; // gives me an error

include "arrays/htmlCode.php" //imports the code from htmlCode.php, but it
//isn't recognized as an array. The page
//displays a ' in front and a ', after each item.

Answer Source

If your file htmlCode.php is delimited by comma(,), then use :

$array = explode(',', file_get_contents('arrays/htmlCode.php'));

If you have to check comma(,) in newline or certain pattern to distinguish array elements, use regular expression.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download