Created
September 18, 2015 14:10
-
-
Save MightyPork/e1fd14557924b556a4eb to your computer and use it in GitHub Desktop.
leftTrimBlock() function
This file contains hidden or 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 | |
/** | |
* Remove left padding space from all lines, | |
* while preserving indentation. | |
* | |
* @param string $txt | |
* @returns string trimmed block of text | |
*/ | |
function leftTrimBlock($txt) | |
{ | |
$pad = 1024; | |
preg_replace_callback('/^( *)(?=[^ \n])/m', function ($m) use (&$pad) { | |
$pad = min($pad, strlen($m[1])); | |
}, $txt); | |
return preg_replace("/^ {{$pad}}/m", '', $txt); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment