CheckeredMichael
CheckeredMichael

Reputation: 571

Replace ereg function to preg_match

I’ve just been hit with an error on a client's website that says ereg has been deprecated and I’ve done a bit of Googling to find that you should use preg_match as it's replacement.

The only problems is, I don’t know much about the preg_match function and I’m also trying to understand this ereg function as a previous employer who is no longer here coded this website.

Any way, here’s the function and I need to know how I should go about replacing it:

if ( ! ereg( '/$', $sCurrentFolder ) ) $sCurrentFolder .= '/' ;

Upvotes: 0

Views: 226

Answers (2)

Giacomo1968
Giacomo1968

Reputation: 26066

Your original ereg seems to only be checking for a forward slash at the end of the string $sCurrentFolder:

if ( ! ereg( '/$', $sCurrentFolder ) ) $sCurrentFolder .= '/' ;

I believe this regex using preg_match should work:

if ( ! preg_match( '/\/$/', $sCurrentFolder ) ) $sCurrentFolder .= '/' ;

EDIT: The original poster indicates this is related to an old version of FCKEditor & I dug up this fairly detailed answer that pinpoints all of the ereg and eregi issues & suggests replacements. Be sure to look trough the whole thread. It’s a very good list and discussion of solutions if you are debugging an old install of FCKEditor. Although I will be honest I am not 100% sure on some of the suggestions presented.

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

That thing's basically saying "if the last character is a slash", so you could use
preg_match("(/$)",$sCurrentFolder) or apply more cleverness:

if( substr($sCurrentFolder,-1) == "/") $sCurrentFolder .= "/";

Or even:

$sCurrentFolder = rtrim($sCurrentFolder,"/")."/";
// This one will remove multiple slashes from the end of $sCurrentFolder
// useful in case of possibly malformed input

Upvotes: 3

Related Questions