Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 794619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:20:31+00:00 2026-05-14T22:20:31+00:00

I’m trying to remove some excessive indention from a string, in this case it’s

  • 0

I’m trying to remove some excessive indention from a string, in this case it’s SQL, so it can be put into a log file. So I need the find the smallest amount of indention (aka tabs) and remove it from the front of each line, but the following code ends up printing out exactly the same, any ideas?

In other words, I want to take the following (NOTE: StackOverflow editor converted my tabs to spaces, in the code, a tab simulates 4 spaces, but it really is a \t character)

        SELECT 
            blah
        FROM
            table
        WHERE
            id=1

and convert it to

SELECT 
    blah
FROM
    table
WHERE
    id=1

here’s the code I tried and fails

$sql = '
        SELECT 
            blah
        FROM
            table
        WHERE
            id=1
';

// it's most likely idented SQL, remove any idention
$lines = explode("\n", $sql);
$space_count = array();
foreach ( $lines as $line )
{
    preg_match('/^(\t+)/', $line, $matches);
    $space_count[] = strlen($matches[0]);
}

$min_tab_count = min($space_count);

$place = 0;
foreach ( $lines as $line )
{
    $lines[$place] = preg_replace('/^\t{'. $min_tab_count .'}/', '', $line);
    $place++;
}

$sql = implode("\n", $lines);

print '<pre>'. $sql .'</pre>';
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:20:31+00:00Added an answer on May 14, 2026 at 10:20 pm

    It seems the problem was

    strlen($matches[0])  
    

    returns 0 and 1 for the first and last line, which isn’t the 3 I actually wanted as the minimum, so a quick hack was to

    • trim the SQL
    • skip counting the length if it’s less than 2

    Not the most elegant solution, but it’ll always work because tabs are usually in the 4+ count in this code. Here’s the fixed code:

    $sql = '
                SELECT 
                    blah
                FROM
                    table
                WHERE
                    id=1
    ';
    
    // it's most likely idented SQL, remove any idention
    $lines = explode("\n", $sql);
    $space_count = array();
    foreach ( $lines as $line )
    {
        preg_match('/^(\t+)/', $line, $matches);
        if ( strlen($matches[0]) > 1 )
        {
            $space_count[] = strlen($matches[0]);
        }
    }
    
    $min_tab_count = min($space_count);
    
    $place = 0;
    foreach ( $lines as $line )
    {
        $lines[$place] = preg_replace('/^\t{'. $min_tab_count .'}/', '', $line);
        $place++;
    }
    
    $sql = implode("\n", $lines);
    
    print $sql;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 476k
  • Answers 476k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes, it's how it works, remember that GL is a… May 16, 2026 at 4:53 am
  • Editorial Team
    Editorial Team added an answer It seems the answer is no. May 16, 2026 at 4:53 am
  • Editorial Team
    Editorial Team added an answer Have you considered Lua? Google docs preview of a pdf… May 16, 2026 at 4:53 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.