Created
July 10, 2018 18:01
-
-
Save w33tmaricich/fbe6ad1d22e9030c050744d1b9fedef7 to your computer and use it in GitHub Desktop.
php: get file contents from regex search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is the correct string. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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