Problem with change from eregi to preg_match

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
HawkHD
Forum Newbie
Posts: 2
Joined: Sun Nov 27, 2011 2:49 pm
Location: Croatia

Problem with change from eregi to preg_match

Post 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!
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Problem with change from eregi to preg_match

Post by McInfo »

Try these.

Code: Select all

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

Code: Select all

if (!preg_match('/.(short|full)./', $file)){
HawkHD
Forum Newbie
Posts: 2
Joined: Sun Nov 27, 2011 2:49 pm
Location: Croatia

Re: Problem with change from eregi to preg_match

Post by HawkHD »

Hi McInfo!
Your suggestion solved the problem. No errors, everything works good now.
Thanks alot!
Post Reply