note 38835 added to function.strspn

From: Date: Thu, 08 Jan 2004 08:01:30 +0000
Subject: note 38835 added to function.strspn
Groups: php.notes 
Request: Send a blank email to php-notes+get-63130@lists.php.net to get a copy of this message
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) / //--------------------------------&#65533; // 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); } ?> ---- Manual Page -- http://www.php.net/manual/en/function.strspn.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+38835 Delete -- http://master.php.net/manage/user-notes.php?action=delete+38835&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+38835&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#63130) next »