Moon-Man.net Posted September 5, 2006 Share Posted September 5, 2006 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: TESTINGDIRFile found: TESTINGFILEFile 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 Quote Link to comment Share on other sites More sharing options...
effigy Posted September 5, 2006 Share Posted September 5, 2006 You're using a string of characters instead of a character class. Try adding a[tt] [ [/tt]after your starting delimiter, and a[tt] ] [/tt]after your ending. Quote Link to comment Share on other sites More sharing options...
Moon-Man.net Posted September 5, 2006 Author Share Posted September 5, 2006 Nup, still no matches :SCheers, Quote Link to comment Share on other sites More sharing options...
effigy Posted September 5, 2006 Share Posted September 5, 2006 [quote]Try adding a [ after your starting delimiter, and a ] after your ending.[/quote]The[tt] ] [/tt]should have been [i]before[/i] your ending delimiter. Can you show me the relevant code change? Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 5, 2006 Share Posted September 5, 2006 whoops... that was my bad (I recognise the script :p)change to:[code]<?php if (preg_match('/[' .preg_quote('\/:*?"<>|', '/') .']+?/', $file)) { ?>[/code]obviously without the <?php ?> tags.ditto for the preg_replace. Quote Link to comment Share on other sites More sharing options...
effigy Posted September 5, 2006 Share Posted September 5, 2006 You also need a[tt] ; [/tt] after[tt] $cnt++[/tt]. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.