Jump to content


Photo

preg_match && preg_quote help


  • Please log in to reply
5 replies to this topic

#1 Moon-Man.net

Moon-Man.net
  • Members
  • PipPipPip
  • Advanced Member
  • 84 posts

Posted 05 September 2006 - 04:09 AM

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

File found: "BAD??BAD::
File found: BAD:
File found: TESTINGDIR
File found: TESTINGFILE
File found: .
File found: BAD?
File found: ..


And there are obviousally charactures in there that are ment to match the the string. Help?
#!/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" ;
?>

Thanks in advance :)
Nathan

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 05 September 2006 - 04:46 AM

You're using a string of characters instead of a character class. Try adding a [ after your starting delimiter, and a ] after your ending.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 Moon-Man.net

Moon-Man.net
  • Members
  • PipPipPip
  • Advanced Member
  • 84 posts

Posted 05 September 2006 - 05:25 AM

Nup, still no matches :S
Cheers,

#4 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 05 September 2006 - 01:18 PM

Try adding a [ after your starting delimiter, and a ] after your ending.


The ] should have been before your ending delimiter. Can you show me the relevant code change?
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#5 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 05 September 2006 - 02:26 PM

whoops... that was my bad (I recognise the script :P)

change to:
<?php if (preg_match('/[' .preg_quote('\/:*?"<>|', '/') .']+?/', $file)) { ?>

obviously without the <?php ?> tags.

ditto for the preg_replace.

#6 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 05 September 2006 - 03:19 PM

You also need a ; after $cnt++.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users