Jump to content

ereg - DEPRECATED


karimali831

Recommended Posts

I've been advised not to use ereg as it may not work sometimes but don't know anything else I can use for the below code:

 

     

        $name = $ds['nickname'];
        if (ereg('[^A-Za-z0-9]', $name)) {
        $func = preg_replace ( '/[^A-Za-z0-9]/', '', $name);
        $name = $func;
return $name;

        }else return $ds['nickname']; 

 

so the above ereg changes $name to alphanumeric.

anything else I can use instead of ereg?

Link to comment
https://forums.phpfreaks.com/topic/202287-ereg-deprecated/
Share on other sites

Ok I use preg_match but it doesn't convert.

 


        $name = $ds['nickname'];
        if (preg_match('[^A-Za-z0-9]', $name)) {
        $func = preg_replace ( '/[^A-Za-z0-9]/', '', $name);
        $name = $func;
    return $name;

        }else{ return $ds['nickname']; }

Link to comment
https://forums.phpfreaks.com/topic/202287-ereg-deprecated/#findComment-1060708
Share on other sites

I'm trying to understand it but I just don't get it.

 

can't someone change the ereg to preg_match please?

 

if (ereg('[^A-Za-z0-9]', $name)

 

to

 

if (preg_match(w/e goes here.)

 

Seriously? Did you look at the link Daniel provided or the manual for preg_match? I have a hard time believing that you look at either and don't understand the simple fix.

 

if (preg_match('/[^A-Za-z0-9]/', $name)

Link to comment
https://forums.phpfreaks.com/topic/202287-ereg-deprecated/#findComment-1060730
Share on other sites

Notice I added the 2 slashes in quotes in the 2nd parameter of preg_replace. The slashes are delimiters and are needed in the 1st and 2nd parameters.

 

        $name = $ds['nickname'];
        if (preg_match('[^A-Za-z0-9]', $name)) {
        $func = preg_replace ( '/[^A-Za-z0-9]/', '//', $name);
        $name = $func;
       return $name;

        }else{ return $ds['nickname']; }

Link to comment
https://forums.phpfreaks.com/topic/202287-ereg-deprecated/#findComment-1060782
Share on other sites

I added delimiters (slashes) to preg_match. Removed slashes from preg_replace, 2nd param.

 

        $name = $ds['nickname'];
        if (preg_match('/[^A-Za-z0-9]/', $name)) {
        $func = preg_replace ( '/[^A-Za-z0-9]/', '', $name);
        $name = $func;
       return $name;

        }else{ return $ds['nickname']; }

Link to comment
https://forums.phpfreaks.com/topic/202287-ereg-deprecated/#findComment-1060788
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.