LoginSignup
6
7

More than 5 years have passed since last update.

特定文字を指定文字数目に挿入したい

Last updated at Posted at 2014-11-10

表題の処理をしたい(例えば、'20141110'の4文字目にハイフンを入れて'2014-1110'にしたいとか)ケースがあったが、簡単に実施する方法がよく分からなかったので、以下のような関数にしてみたが、どうなんだろう。。

insert.php
<?php

/**
 * 
 * @param type $text1 元の文字列
 * @param type $num 挿入先の文字数目
 * @param type $text2 挿入文字
 */
function insertStr($text1, $num, $text2){
    return substr($text1, 0, $num).$text2.substr($text1, $num, strlen($text1));
}

echo insertStr("20141110", 4,  "-");//string(2014-1110)
6
7
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7