Created
September 29, 2011 11:24
-
-
Save kamekoopa/1250557 to your computer and use it in GitHub Desktop.
指定した見た目の文字幅(≠文字数。半角1 全角2)ごとに、指定した文字を挟み込むPHP関数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function mb_insertstr_per_charwidth($str, $width, $insert, $encode){ | |
$buf = ""; | |
$lines = array(); | |
for($i=0; $i<mb_strlen($str, $encode); $i++){ | |
$char = mb_substr($str, $i, 1, $encode); | |
if(mb_strwidth($buf . $char, $encode) <= $width){ | |
$buf .= $char; | |
}else{ | |
$lines[] = $buf; | |
$buf = $char; | |
} | |
} | |
$lines[] = $buf; | |
return implode($insert, $lines); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
「エンコード未指定時は内部文字エンコードを利用します」というmb系関数のお約束入れ忘れた。