Home > Backend Development > PHP Tutorial > Examples of mutual conversion between numerical ordinal and alphabetical numbers for PHP learning

Examples of mutual conversion between numerical ordinal and alphabetical numbers for PHP learning

little bottle
Release: 2023-04-06 10:52:02
forward
2328 people have browsed it

The editor of this article will share with you a code example of how to use PHP to realize the mutual conversion of numerical ordinal numbers and alphabetical ordinal numbers. It has certain reference value. Friends who are interested can take a look. I hope it will be helpful to you. inspired by.

OrderThe number starts from 1, that is, A=1

/**
   * 数字序列转字母序列
   * @param $int
   * @param int $start
   * @return string|bool
   */
  function int_to_chr_1($int, $start = 64)
  {
    if (!is_int($int) || $int <= 0) return false;
    $str = &#39;&#39;;
    if (floor($int / 26) > 0) {
      $str .= int_to_chr_1((int)floor($int / 26));
    }
    return $str . chr($int % 26 + $start);
  }

  /**
   * 数字序列转字母序列
   * @param $int
   * @return string|bool
   */
  function int_to_chr_2($int)
  {
    if (!is_int($int) || $int <= 0) return false;

    $array = array(&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;, &#39;G&#39;, &#39;H&#39;, &#39;I&#39;, &#39;J&#39;, &#39;K&#39;, &#39;L&#39;, &#39;M&#39;, &#39;N&#39;, &#39;O&#39;, &#39;P&#39;, &#39;Q&#39;, &#39;R&#39;, &#39;S&#39;, &#39;T&#39;, &#39;U&#39;, &#39;V&#39;, &#39;W&#39;, &#39;X&#39;, &#39;Y&#39;, &#39;Z&#39;);
    $str = &#39;&#39;;
    if ($int > 26) {
      $str .= int_to_chr_2((int)floor($int / 26));
      $str .= $array[$int % 26 - 1];
      return $str;
    } else {
      return $array[$int - 1];
    }
  }

  /**
   * 字母序列转数字序列
   * @param $char
   * @return int|bool
   */
  function chr_to_int($char)
  {
    //检测字符串是否全字母
    $regex = &#39;/^[a-zA-Z]+$/i&#39;;

    if (!preg_match($regex, $char)) return false;

    $int = 0;
    $char = strtoupper($char);
    $array = array(&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;, &#39;G&#39;, &#39;H&#39;, &#39;I&#39;, &#39;J&#39;, &#39;K&#39;, &#39;L&#39;, &#39;M&#39;, &#39;N&#39;, &#39;O&#39;, &#39;P&#39;, &#39;Q&#39;, &#39;R&#39;, &#39;S&#39;, &#39;T&#39;, &#39;U&#39;, &#39;V&#39;, &#39;W&#39;, &#39;X&#39;, &#39;Y&#39;, &#39;Z&#39;);
    $len = strlen($char);
    for ($i = 0; $i < $len; $i++) {
      $index = array_search($char[$i], $array);
      $int += ($index + 1) * pow(26, $len - $i - 1);
    }
    return $int;
  }


  echo &#39;<br>&#39;, int_to_chr_1(8848);
  echo &#39;<br>&#39;, int_to_chr_2(8848);
  echo &#39;<br>&#39;, chr_to_int(&#39;MBH&#39;);
Copy after login

Related tutorials:PHP Video tutorial

The above is the detailed content of Examples of mutual conversion between numerical ordinal and alphabetical numbers for PHP learning. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template