Jump to content

preg_match && preg_quote help


Moon-Man.net

Recommended Posts

Hey,
I have this script that is ment to replace move any files that contain charactures that are in the preg_quote part.
But i can never get it to match. i get this output

[quote]
File found: "BAD??BAD::
File found: BAD:
File found: TESTINGDIR
File found: TESTINGFILE
File found: .
File found: BAD?
File found: ..
[/quote]

And there are obviousally charactures in there that are ment to match the the string. Help?
[code]
#!/usr/bin/php
<?PHP
$dir = '/tmp/TESTING/';
$handle = opendir($dir);
$cnt=0;

echo "Starting Replace!\n" ;
while(($file = readdir($handle)) !== false){
// echo "File found: " .$file ."\n";
    if (preg_match('/' .preg_quote('\/ : * ? " < > |', '/') .'/', $file)){
echo "Match Found: " .$file ;
$cnt++
        $path = $dir . DIRECTORY_SEPARATOR . $file;
        if (!in_array($file, array('..', '.')) && is_file($path)){
            $contents = file_get_contents($path);
            $newPath = $dir . DIRECTORY_SEPARATOR . preg_replace('/' . preg_quote('\/ : * ? " < > |', '/') . '/', '_', $file);
            if (file_put_contents($newPath, $contents) !== false){
                $unlink($path); //delete old file only if able to create new
            }
        }
    }
}
echo "Replace finnished" ;
echo "There were " .$cnt ." Replacements Made" ;
?>
[/code]

Thanks in advance :)
Nathan
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.