Title: Unindent code
Last modified: August 22, 2016

---

# Unindent code

 *  [partage-it](https://wordpress.org/support/users/partage-it/)
 * (@partage-it)
 * [11 years, 8 months ago](https://wordpress.org/support/topic/unindent-code/)
 * Hello again again 😉
 * I have another request: when displaying an extract, the base indentation is not
   useful for the extract and should be removed.
 * For example, instead of displaying this code in a condition in a loop in a method
   in a class:
 *     ```
       //
       				if ($firstConnection) {
       					echo "hello world!";
       				}
       ```
   
 * it should be more interesting to display:
 *     ```
       if ($firstConnection) {
       	echo "hello world!";
       }
       ```
   
 * That’s all… for now 😉
 * David
 * [https://wordpress.org/plugins/pastacode/](https://wordpress.org/plugins/pastacode/)

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Author [Willy Bahuaud](https://wordpress.org/support/users/willybahuaud/)
 * (@willybahuaud)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/unindent-code/#post-5303233)
 * Hello,
 * I try a little function, but I encountered a problem. I need do determine, for
   each code, tab length…
 *     ```
       <?php
       $var = '	// this is a test
       	with a second line
       	//and a third line';
   
       $regex = '/(^\t|^:space:+)/';
       $count = array();
       $var2 = explode( "\n", $var );
       $mini = PHP_INT_MAX;
       foreach( $var2 as $v ) {
       	if ( ! preg_match( $regex, $v, $match ) ) {
       		$mini = PHP_INT_MAX;
       		break;
       	} elseif ( strlen( $match[0] ) < $mini ) {
       		$mini = strlen( $match[0] ); //how many spaces for a tab ?
       	}
       }
       if ( $mini != PHP_INT_MAX ) {
       	$regex2 = '/(^\t|^:space:{' . $mini . '})/';
       	$regex4 = '/\n\t|\n:space:{' . $mini . '}/';
       	$var = preg_replace( $regex2, "", $var );
       	$var = preg_replace( $regex4, "\n", $var );
       }
       // $var is now indented correctly
       ```
   
 *  Thread Starter [partage-it](https://wordpress.org/support/users/partage-it/)
 * (@partage-it)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/unindent-code/#post-5303236)
 * Yes, it is not simple 🙂
 * A simple solution is maybe to remove first white char (space or \t) of every 
   lines until at least one line first char is not a white char.
 * For example:
 *     ```
       __a
       ____b
       ______c
       ```
   
 * becomes in the first iteration:
 *     ```
       _a
       ___b
       _____c
       ```
   
 * and finally:
 *     ```
       a
       __b
       ____c
       ```
   
 * David
 *  Thread Starter [partage-it](https://wordpress.org/support/users/partage-it/)
 * (@partage-it)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/unindent-code/#post-5303238)
 * Hello Willy,
 * As i am not confident with regexp, so i have tried with a simple class to handle
   code indentation.
 * The result is here, if you need:
    [https://gist.github.com/partageit/869fa0fbf9f89582d5f0](https://gist.github.com/partageit/869fa0fbf9f89582d5f0)
 * David

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Unindent code’ is closed to new replies.

 * ![](https://ps.w.org/pastacode/assets/icon-256x256.png?rev=2819151)
 * [Pastacode](https://wordpress.org/plugins/pastacode/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/pastacode/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/pastacode/)
 * [Active Topics](https://wordpress.org/support/plugin/pastacode/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/pastacode/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/pastacode/reviews/)

## Tags

 * [code](https://wordpress.org/support/topic-tag/code/)
 * [indentation](https://wordpress.org/support/topic-tag/indentation/)

 * 3 replies
 * 2 participants
 * Last reply from: [partage-it](https://wordpress.org/support/users/partage-it/)
 * Last activity: [11 years, 4 months ago](https://wordpress.org/support/topic/unindent-code/#post-5303238)
 * Status: not resolved