karimali831 Posted May 19, 2010 Share Posted May 19, 2010 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? Quote Link to comment Share on other sites More sharing options...
ale8oneboy Posted May 19, 2010 Share Posted May 19, 2010 From my understanding is that preg_match is the alternative replacement. http://www.php.net/manual/en/function.preg-match.php Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted May 19, 2010 Share Posted May 19, 2010 Use PCRE. See http://dk.php.net/manual/en/reference.pcre.pattern.posix.php for a conversion guide. Quote Link to comment Share on other sites More sharing options...
karimali831 Posted May 19, 2010 Author Share Posted May 19, 2010 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']; } Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted May 19, 2010 Share Posted May 19, 2010 You didn't add delimiters in your preg_match(). Quote Link to comment Share on other sites More sharing options...
karimali831 Posted May 19, 2010 Author Share Posted May 19, 2010 I'm not familiar with.. perhaps you could help me out? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted May 19, 2010 Share Posted May 19, 2010 It's explained right here: http://dk.php.net/manual/en/regexp.reference.delimiters.php Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 19, 2010 Share Posted May 19, 2010 @Daniel, C'mon, you can't actually expect people to click on the links you provide. That would require actual work on their part: reading, comprehension, etc. Quote Link to comment Share on other sites More sharing options...
karimali831 Posted May 19, 2010 Author Share Posted May 19, 2010 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.) Quote Link to comment Share on other sites More sharing options...
Maq Posted May 19, 2010 Share Posted May 19, 2010 Maybe this will help: int preg_match ( string $pattern , string $subject ) Quote Link to comment Share on other sites More sharing options...
karimali831 Posted May 19, 2010 Author Share Posted May 19, 2010 Not really, besides it needs to start with if(preg_match(... Never used this before so I don't know what the delimiters are. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 19, 2010 Share Posted May 19, 2010 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) Quote Link to comment Share on other sites More sharing options...
bulrush Posted May 19, 2010 Share Posted May 19, 2010 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']; } Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted May 19, 2010 Share Posted May 19, 2010 The replacement string should not be delimited. Quote Link to comment Share on other sites More sharing options...
bulrush Posted May 19, 2010 Share Posted May 19, 2010 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']; } Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 19, 2010 Share Posted May 19, 2010 That is apparently in a function. Why are you making it so complicated? Your function simply needs a single line to do this: function foo() { return preg_replace('/[^A-Za-z0-9]/', '', $ds['nickname']); } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.