Page 1 of 1

Problem with change from eregi to preg_match

Posted: Sun Nov 27, 2011 2:59 pm
by HawkHD
Hello.

I'm having a problem with changing a function which uses eregi() into a preg_match() to gain compatiblity with PHP 5.3 .
Here is the orginal function with eregi

Code: Select all

function cache_remover($alone = ''){

	$fdir = opendir(rootpath.'/cache');
	while ($file = readdir($fdir)){
		if ($file != '.' and $file != '..' and $file != '.htaccess'){
			if ($alone){
				if (eregi($alone.'(.*).(short|full).', $file)){
					@unlink(rootpath.'/cache/'.$file);
				}

				if (!eregi('.(short|full).', $file)){
					@unlink(rootpath.'/cache/'.$file);
				}
			}

			if (!$alone){
				@unlink(rootpath.'/cache/'.$file);
			}
		}
	}

return true;
}
I have modified the function into this:

Code: Select all

function cache_remover($alone = ''){

	$fdir = opendir(rootpath.'/cache');
	while ($file = readdir($fdir)){
		if ($file != '.' and $file != '..' and $file != '.htaccess'){
			if ($alone){
				if (preg_match('/$alone/'.'(.*).(short|full).', $file)){
				@unlink(rootpath.'/cache/'.$file);
				}
				// if (!preg_match('/(short|full)/', $file)){
				if (!preg_match('.(short|full).', $file)){
					@unlink(rootpath.'/cache/'.$file);
				}
			}
			if (!$alone){
			@unlink(rootpath.'/cache/'.$file);
			}
		}
	}
	
	return true;
}
however, I'm getting an error Warning: preg_match() [function.preg-match]: Unknown modifier '(' in ... on line

Code: Select all

if (preg_match('/$alone/'.'(.*).(short|full).', $file)){
.

Does someone see what I'm doing wrong and how it should be written?

Thanks in advance!

Re: Problem with change from eregi to preg_match

Posted: Sun Nov 27, 2011 6:34 pm
by McInfo
Try these.

Code: Select all

if (preg_match('/'.$alone.'(.*).(short|full)./', $file)){

Code: Select all

if (!preg_match('/.(short|full)./', $file)){

Re: Problem with change from eregi to preg_match

Posted: Mon Nov 28, 2011 1:40 pm
by HawkHD
Hi McInfo!
Your suggestion solved the problem. No errors, everything works good now.
Thanks alot!