Skip to content

Instantly share code, notes, and snippets.

@MightyPork
Created September 18, 2015 14:10
Show Gist options
  • Save MightyPork/e1fd14557924b556a4eb to your computer and use it in GitHub Desktop.
Save MightyPork/e1fd14557924b556a4eb to your computer and use it in GitHub Desktop.
leftTrimBlock() function
<?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