开发者

PHP replacing entire string if it contains integer

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 its开发者_StackOverflow社区elf and not the entire line. Any help would be appreciated.


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

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜