好玩的线上检测代码工具-codewars(4)

来源:互联网 发布:优酷会员淘宝不能买了 编辑:程序博客网 时间:2024/05/22 08:27

You are going to be given a word. Your job is to return the middle character of the word. If the word’s length is odd, return the middle character. If the word’s length is even, return the middle 2 characters.

Examples:

Kata.getMiddle(“test”) should return “es”

Kata.getMiddle(“testing”) should return “t”

Kata.getMiddle(“middle”) should return “dd”

Kata.getMiddle(“A”) should return “A”
Input

A word (string) of length 0 < str < 1000 (In javascript you may get slightly more than 1000 in some test cases due to an error in the test cases). You do not need to test for this. This is only here to tell you that you do not need to worry about your solution timing out.

Output

The middle character(s) of the word represented as a string.

function getMiddle($text) {  $len = strlen($text);  if($len <= 0 || $len >= 1000)  {    return '';  }  //偶数直接返回两个字符  if($len % 2 == 0)  {      return substr($text, $len/2-1, 2);  }  else  {    return substr($text, floor($len/2), 1);  }}