note 38835 deleted from function.strspn by nlopess
| From: | nlopess@php.net | Date: | Thu, 08 Jan 2004 16:11:08 +0000 |
| Subject: | note 38835 deleted from function.strspn by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-63154@lists.php.net to get a copy of this message | ||
Note Submitter: hph@mechintosh.com
----
This was for an online file editor. Tabs in files are icky in textareas so it just replaced those by
2 spaces each, and when writing the file back to disk, all leading runs of spaces were replaced by
tabs again (so I could still use the regular editor as well)
<?php
//__________________________________,
// lsp2tb($string, $tablength = 2) /
//--------------------------------�
// that's Leading SPace to TaBs
function lsp2tb($string, $tablength = 2) {
$string = nl2unix($string); // make lineendings \n
$output = '';
foreach(explode("\n", $string) as $line) {
$len = floor(strspn($line, " ") / $tablength);
$output .= str_repeat("\t", $len) . substr($line, $tablength * $len) . "\n";
}
return $output;
}
function nl2unix($string) {
return str_replace(array("\r\n", "\r"), "\n", $string);
}
?>