paulman888888 Posted May 9, 2008 Share Posted May 9, 2008 Hi, I want letter to be replaced with a number eg a -->> 1 b -->> 2 and so on. Can anyone tell me how to do it please. Thankyou very much Quote Link to comment Share on other sites More sharing options...
Spaceman-Spiff Posted May 9, 2008 Share Posted May 9, 2008 You can use str_replace() function, or if you want to put number to all the alphabet letters, you can use ord() instead. Quote Link to comment Share on other sites More sharing options...
moselkady Posted May 9, 2008 Share Posted May 9, 2008 try : <?php $a = ord('a')-1; $letter = 'x'; echo ord($letter)-$a; $letter = 'z'; echo ord($letter)-$a; ?> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 9, 2008 Share Posted May 9, 2008 $letters = range("a-z"); $numbers = range("1-26"); foreach ($letters as $k=>$v) { $newarr[] = $numbers[$k]; } I think that'll work. Didn't test it though. Quote Link to comment Share on other sites More sharing options...
moselkady Posted May 9, 2008 Share Posted May 9, 2008 I will go with DarkWater's method Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 9, 2008 Share Posted May 9, 2008 And it'll work for any set of letters, by the way, if I change it up a bit. Here: $str = "There are some letters here!"; $letters = range("a-z"); $numbers = range("1-26"); $str = str_split("", $str); array_walk($str, 'strtolower'): foreach ($str as $k=>$v) { $pos = array_search($v, $letters); $newarr[] = $numbers[$pos]; } $newstr = implode("", $newarr); echo $newstr; Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 9, 2008 Share Posted May 9, 2008 function remove_nonalpha($str) { $let = range('a-z'); $str = strtolower($str); if (!in_array($str, $let)) { return NULL; } return $str; } $str = "There are some letters here!"; $str = str_replace($bad, "", $str); $letters = range("a-z"); $numbers = range("1-26"); $str = str_split("", $str); array_walk($str, 'remove_nonalpha'): foreach ($str as $k=>$v) { $pos = array_search($v, $letters); $newarr[] = $numbers[$pos]; } $newstr = implode("", $newarr); echo $newstr; Fixed it to remove non-letter characters. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 9, 2008 Share Posted May 9, 2008 Rewritten a 3rd time to let any numbers you currently have in there to stay AND to not recreate a $let array every time (saves resources). You can remove the number thing if you want. function remove_nonalpha($str, $key, $let = "") { if (!isset($let)) { $let = range("a-z"); } $str = strtolower($str); if (!in_array($str, $let) || !is_numeric($str)) { return NULL; } return $str; } $str = "There are some letters here!"; $str = str_replace($bad, "", $str); $letters = range("a-z"); $numbers = range("1-26"); $str = str_split("", $str); array_walk($str, 'remove_nonalpha', $letters): foreach ($str as $k=>$v) { $pos = array_search($v, $letters); $newarr[] = $numbers[$pos]; } $newstr = implode("", $newarr); echo $newstr; Quote Link to comment Share on other sites More sharing options...
paulman888888 Posted May 10, 2008 Author Share Posted May 10, 2008 thank you very much 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.