Jump to content

[SOLVED] replacing space indents with tab indents on new lines


ultrus

Recommended Posts

Hello,

I have php documents that use spaces to indent the code, and am planning on using Dreamweaver's find and replace tool with regular expressions to replace the spaces with tabs. How would I find \n following any number of spaces with \n and number of tabs? I don't want to replace all spaces in the document with tabs of course. (If the answer is in php script, that's fine with me) :)

 

Thanks for the assist!

Link to comment
Share on other sites

<pre>
<?php
### Space = #32; Tab = #9.

### How many spaces is a tab?
$tab = 8;

$data = <<<DATA
  2
    4
      6
        8
          10
            12
              14
                16
DATA;

echo preg_replace('/[ \t]/', 'X', $data), '<br>';
print_r(count_chars($data, 1));

echo '<hr>';
$data = preg_replace('/^([ ]+)/me', 'str_repeat("\t", strlen("$1") / $tab)', $data);

echo preg_replace('/[ \t]/', 'X', $data), '<br>';
print_r(count_chars($data, 1));
?>
</pre>

 

XX2

XXXX4

XXXXXX6

XXXXXXXX8

XXXXXXXXXX10

XXXXXXXXXXXX12

XXXXXXXXXXXXXX14

XXXXXXXXXXXXXXXX16

Array

(

    [10] => 7

    [13] => 7

    [32] => 72

    [48] => 1

    [49] => 4

    [50] => 2

    [52] => 2

    [54] => 2

    [56] => 1

)

-------------------------------------------

2

4

6

X8

X10

X12

X14

XX16

Array

(

    [9] => 6

    [10] => 7

    [13] => 7

    [48] => 1

    [49] => 4

    [50] => 2

    [52] => 2

    [54] => 2

    [56] => 1

)

Link to comment
Share on other sites

Hello,

I have php documents that use spaces to indent the code, and am planning on using Dreamweaver's find and replace tool with regular expressions to replace the spaces with tabs. How would I find \n following any number of spaces with \n and number of tabs? I don't want to replace all spaces in the document with tabs of course. (If the answer is in php script, that's fine with me) :)

 

Thanks for the assist!

 

In Dreamweaver, from the top menu, select Edit > Preferences...

In the left category column, scroll down till you see 'code format', click on it.. in the right hand panel, you should see at the top an indent with a checkmark, followed by a number field and a dropdown menu (the default will most likely be 2 in the field and the dropdown set to spaces).. set the field to 1, and set the dropdown to tabs. Click 'OK'.

 

Finally, in the top menu, select Commands > Apply Source Formatting.

 

Done.  ;)

 

Cheers,

 

NRG

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.