Created
February 10, 2012 15:21
-
-
Save rvanvelzen/1790237 to your computer and use it in GitHub Desktop.
PHP unindent
This file contains 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 | |
/** | |
* Unindent a given string | |
* | |
* Empty lines are skipped. | |
* | |
* N.b.: de longest common substring is used, as a result all the | |
* indenting has to be exactly the same. Proper unindenting may fail | |
* otherwise. | |
* | |
* @param string $text | |
* @return string | |
*/ | |
function unindent($text) { | |
if (preg_match('{\A[\r\n]*(\h+)[^\r\n]*+(?:[\r\n]++(?>\1[^\r\n]*+(?:[\r\n]+|\z)|[\r\n]+)+)?\z}', rtrim($text), $match)) { | |
$text = preg_replace('{^' . $match[1] . '}m', '', $text); | |
} | |
return $text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment