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 Link to comment https://forums.phpfreaks.com/topic/104917-solved-replace-help/ 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. Link to comment https://forums.phpfreaks.com/topic/104917-solved-replace-help/#findComment-537012 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; ?> Link to comment https://forums.phpfreaks.com/topic/104917-solved-replace-help/#findComment-537018 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. Link to comment https://forums.phpfreaks.com/topic/104917-solved-replace-help/#findComment-537021 Share on other sites More sharing options...
moselkady Posted May 9, 2008 Share Posted May 9, 2008 I will go with DarkWater's method Link to comment https://forums.phpfreaks.com/topic/104917-solved-replace-help/#findComment-537032 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; Link to comment https://forums.phpfreaks.com/topic/104917-solved-replace-help/#findComment-537047 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. Link to comment https://forums.phpfreaks.com/topic/104917-solved-replace-help/#findComment-537054 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; Link to comment https://forums.phpfreaks.com/topic/104917-solved-replace-help/#findComment-537065 Share on other sites More sharing options...
paulman888888 Posted May 10, 2008 Author Share Posted May 10, 2008 thank you very much Link to comment https://forums.phpfreaks.com/topic/104917-solved-replace-help/#findComment-537386 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.