1
+ <?php
2
+ /*
3
+ * 用字母表数字替换字符串的字母
4
+ * In this kata you are required to, given a string, replace every letter with its position in the alphabet.
5
+ * If anything in the text isn't a letter, ignore it and don't return it.
6
+ * As an example:
7
+ alphabet_position('The sunset sets at twelve o\' clock.');
8
+ Should return "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11" as a string.
9
+ */
10
+ function alphabet_position (string $ s ): string
11
+ {
12
+ // 字母表数组
13
+ $ letterArr = [1 => 'a ' ,'b ' ,'c ' ,'d ' ,'e ' ,'f ' ,'g ' ,'h ' ,'i ' ,'j ' ,'k ' ,'l ' ,'m ' ,'n ' ,'o ' ,'p ' ,'q ' ,'r ' ,'s ' ,'t ' ,'u ' ,'v ' ,'w ' ,'x ' ,'y ' ,'z ' ];
14
+ // 将参数$s全部小写, 转换为目标数组
15
+ $ targetArr = str_split (strtolower ($ s ));
16
+ $ result = '' ;
17
+ // 循环目标数组
18
+ foreach ($ targetArr as $ value ) {
19
+ // 循环字母表数组
20
+ foreach ($ letterArr as $ k =>$ v ) {
21
+ // 比较两个数组中值,若相等, 取出字母表数组中键
22
+ if ($ value == $ v ) {
23
+ $ result .= $ k .'' ;
24
+ }
25
+ }
26
+ }
27
+ // 去除右边空格.
28
+ return rtrim ($ result );
29
+ }
30
+
31
+ function alphabet_position_clever (string $ s ): string
32
+ {
33
+ $ value = '' ;
34
+ // 根据正则表达式排除非字符的字符串, 转换为目标数组, 并循环
35
+ foreach (str_split (preg_replace ('/[^a-z]/i ' , '' , $ s )) as $ letter ) {
36
+ // 求每个大写英文字符 ASCII 码值.
37
+ // A在ASCII表是第65,依次排序25个字母,所以要减去64得到26个字母表正确数字
38
+ // https://baike.baidu.com/pic/ASCII/309296/0/c2fdfc039245d688c56332adacc27d1ed21b2451?fr=lemma&ct=single#aid=0&pic=c2fdfc039245d688c56332adacc27d1ed21b2451
39
+ $ letterToNumber = ord (strtoupper ($ letter )) - 64 ;
40
+ // 拼接字符串
41
+ $ value .= "{$ letterToNumber }" ;
42
+ }
43
+ return trim ($ value );
44
+ }
0 commit comments