views:

59

answers:

2

My script lists out files in the directory. I am able to use preg_match and regex to find files whose filenames contain integers.

However, this is what I am unable to do: I want an entire string to be omitted if it contains an integer.

Despite trying several methods, I am only able to replace the integer itself and not the entire line. Any help would be appreciated.

+6  A: 
if (preg_match('/\d/', $string))
    $string = "";

This will turn a string into an empty one if it has any number in it.

Artefacto
He said "I want an entire string to be omitted if it contains ..." not "set to zero length".
rubber boots
@rub doesn't matter. If he's going to append the resulting string to a file/echo it, appending an empty string is the same as appending nothing.
Artefacto
Artefacto, from his description, it looks like he has already an array with filenames.
rubber boots
A: 

According to your description, this should be sth. like:

$files = array();

$dirname = 'C://Temp';
$dh = opendir($dirname) or die();

while( ($fn=readdir($dh)) !== false ) 
    if( !preg_match('/\d+|^\.\.?$/', $fn) )
       $files[] = $fn;

closedir($dh);

var_dump($files);

... which reads all file names and stores them (except these with numbers and ../.) in an array '$files', which itself gets displayed at the end of the snipped above. If that doesn't fit your requirement, you should give a more detailed explanation of what you are trying to do

Regards

rbo

rubber boots