Skip to content

Instantly share code, notes, and snippets.

@srsbiz
Last active December 11, 2015 01:49
Show Gist options
  • Save srsbiz/4526564 to your computer and use it in GitHub Desktop.
Save srsbiz/4526564 to your computer and use it in GitHub Desktop.
Multibyte friendly tab to space replacer
<?php
/**
* Multibyte friendly tab replacer.
* @param string $line Line with tabs to convert
* @param int $tabsize Number of space characters in full width tab
* @return string Line without tabs
*/
function tab2space($line, $tabsize = 4)
{
while (($t = mb_strpos($line, "\t")) !== false) {
$preTab = $t ? mb_substr($line, 0, $t) : '';
$line = $preTab . str_repeat(' ', $tabsize - (mb_strlen($preTab) % $tabsize)) . mb_substr($line, $t + 1);
}
return $line;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment