Skip to content

Instantly share code, notes, and snippets.

@w33tmaricich
Created July 10, 2018 18:01
Show Gist options
  • Save w33tmaricich/fbe6ad1d22e9030c050744d1b9fedef7 to your computer and use it in GitHub Desktop.
Save w33tmaricich/fbe6ad1d22e9030c050744d1b9fedef7 to your computer and use it in GitHub Desktop.
php: get file contents from regex search
This is the correct string.
<?php
// Get the list of files in the current directory.
$directoryContents = scandir(".");
print_r($directoryContents);
// Use a regex to find the one you are looking for.
$matching = array_values(preg_grep('/(.\.file)/', $directoryContents));
print_r($matching);
// If you find it, grab the contents of the file.
if ($matching) {
$fileContents = file_get_contents($matching[0]);
} else {
$fileContents = "The file was not found.";
}
echo $fileContents;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment