I have this regex:
'/^files\/(.+\..+)$/';
But I want to replace the "files/" section with a runtime variable.
I tried this:
$filePath = "files/";
'/^'.$filePath.'(.+\..+)$/';
which didn't work, as well as this:
$filePath = preg_quote("files/");
'/^'.$filePath.'(.+\..+)$/';
but I still get an error in this loop on the preg_match line, saying that "(" is an unknown modifier.
foreach (glob(FILE_PATH."*.*") as $filename) {
preg_match($pattern, $filename, $matches);
echo "<option class='file' value='".$matches[1]."'>".$matches[1]."</option>";
}
Any help would be appreciated, thanks.
EDIT
This works...
$filePath = 'files\/';
...but in actual usage, I'm trying to use a constant declared in another file.
So:
define('FILE_PATH', 'files/');
...and then trying to use that constant. It can't have the escape character embedded because some other parts of the application need it without the escape character.
You need to hand preg_quote the delimiter:
$filePath = preg_quote("files/", '/');
Since you can use a lot of characters as delimiters, preg_quote cannot know, that you are going to use /, so it does not escape it by default. That's what the second (optional) parameter is for.
Otherwise, your second approach (the one using preg_quote) is the way to go.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With