1

I am trying to loop through all the php files listed in an array called $articleContents and extract the variables $articleTitle and $heroImage from each.

So far I have the following code:

$articleContents = array("article1.php", "article2.php"); // array of all file names
$articleInfo = [];
$size = count($articleContents);
for ($x = 0; $x <= $size; $x++) {
ob_start();

if (require_once('../articles/'.$articleContents[$x])) {

ob_end_clean();

    $entry = array($articleContents[$x],$articleTitle,$heroImage);

    array_push($articlesInfo, $entry);

}

The problem is, the php files visited in the loop have html, and I can't keep it from executing. I would like to get variables from each of these files without executing the html inside each one.

Also, the variables $articleTitle and $heroImage also exist at the top of the php file I'm working in, so I need to make sure the script knows I'm calling the variables in the external file and not the current one.

If this is not possible, can you please recommend an alternative method?

Thanks!

1
  • Trying to get file contents as a side effect of having PHP load the files is odd. You should either move the variables into a separate file as defines, or use something like file_get_contents() and parse the string. Commented Jun 26, 2017 at 1:39

3 Answers 3

1

Don't do this.

Your PHP scripts should be for your application, not for your data. For your data, if you want to keep it file-based, use a separate file.

There are plenty of formats to choose from. JSON is quite popular. You can use PHP's built-in serialization as well, which has support for more PHP-native types but is not as portable to other frameworks.

Sign up to request clarification or add additional context in comments.

Comments

0

A little hacky but seems to works:

$result = eval(
  'return (function() {?>' .
  file_get_contents('your_article.php') .
  'return [\'articleTitle\' => $articleTitle, \'heroImage\' => $heroImage];})();'
);

Where your_article.php is something like:

<?php

$articleTitle = 'hola';
$heroImage = 'como te va';

The values are returned in the $result array.

Explanation:

Build a string of php code where the code in your article scripts are wrapped inside a function that returns an array with the values you want.

function() {
  //code of your article.php
  return ['articleTitle' => $articleTitle, 'heroImage' => $heroImage];
}

Maybe you must do some adaptations to the strings due <?php ?> tags placements.

Anyway, this stuff is ugly. I'm very sure that it can be refactored in some way.

Comments

0

Your problem (probably) comes down to using parentheses with require. See the example and note here.

Instead, format your code like this

$articlesInfo = []; // watch your spelling here
foreach ($articleContents as $file) {
    ob_start();
    if (require '../articles/' . $file) { // note, no parentheses around the path
        $articlesInfo[] = [
            $file,
            $articleTitle,
            $heroImage
        ];
    }
    ob_end_clean();
}

Update: I've tested this and it works just fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.