search

Home  >  Q&A  >  body text

PHP uses one string to modify characters in another string

阿神阿神2743 days ago277

reply all(2)I'll reply

  • 阿神

    阿神2017-05-16 13:03:01

    In PHP, a string is an array of bytes. Use curly braces and numeric subscripts to access or modify the corresponding single character in the string. Note that it is a single character!

    $str{1} in your code can only hold one character, and your assignment is 5 characters. At this time, php will only take the first character by default, so its result will naturally be lnmp.

    In addition, the official PHP documentation also says that using curly braces to access or modify strings is very unsafe for multi-byte character sets. Try not to use {} when you use other encoding sets such as UTF-8 or GBK.

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-16 13:03:01

    String string

    A string string is composed of a series of characters, where each character is equivalent to one byte. This means that PHP can only support a character set of 256 and therefore does not support Unicode. See the detailed explanation of string types for details.

    Quoted from the PHP manual. Indicated in bold. Each character is a byte. Therefore, only one byte position can be replaced.

    reply
    0
  • Cancelreply