Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
英国の郵便番号の長さに応じて、PHPを使用して文字列の文字間にスペースを挿入する方法を教えてもらえますか?
たとえば、文字列が5つの用船者の場合、2番目の文字の後にスペースを挿入しますか?6文字の場合は3番目の後に挿入しますか?
正規表現を使用する: $formatted = preg_replace('/([a-Z0-9]{3})$/', ' \1', $postalCode); これは英数字でのみ機能することに注意してください。ただし、入力の範囲はそれであると想定しています。
$formatted = preg_replace('/([a-Z0-9]{3})$/', ' \1', $postalCode);