Skip to content

Instantly share code, notes, and snippets.

@kamekoopa
Created September 29, 2011 11:24
Show Gist options
  • Save kamekoopa/1250557 to your computer and use it in GitHub Desktop.
Save kamekoopa/1250557 to your computer and use it in GitHub Desktop.
指定した見た目の文字幅(≠文字数。半角1 全角2)ごとに、指定した文字を挟み込むPHP関数
<?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);
}
@kamekoopa
Copy link
Author

「エンコード未指定時は内部文字エンコードを利用します」というmb系関数のお約束入れ忘れた。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment