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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.