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.

1
  • 1
    You need to post your code too. Commented Aug 15, 2010 at 19:26

2 Answers 2

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

He said "I want an entire string to be omitted if it contains ..." not "set to zero length".
@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, from his description, it looks like he has already an array with filenames.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.