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 7846909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:44:56+00:00 2026-06-02T17:44:56+00:00

I am working to clean up CSS output with str_replace, but need to ignore

  • 0

I am working to clean up CSS output with str_replace, but need to ignore certain block of strings with a defined start and end.

Ignore anything that starts with:

@media blah(this can be any value) {

content to ignore:

  .class {
    property: value;
  }

and ends with:

}/*ends*/

And do str_replace outside that block instead:

.otherclass {property:value;}

PHP:

$output = str_replace("{", "{\n", $output);
$output = str_replace("}", "}\n", $output);

The reason is that @media block need proper indentation which is already correctly formatted, and should not be touched by str_replace which simple return readable lines:

 .otherclass {
  property:value;
  }

How would you say it in PHP?

Thanks

  • 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-06-02T17:44:57+00:00Added an answer on June 2, 2026 at 5:44 pm
    $string = 
    "@media blah(this can be any value) {
    .class {
        property: value;
      }
    }/*ends*/
    
    .otherclass {property:value;}
    
    .otherclass {property:value;}
    
    @media blah(this can be any value) {
    .class {
        property: value;
      }
    }/*ends*/
    
    .otherclass {property:value;}
    
    .otherclass {property:value;}";
    
    $arr = explode("\r", $string);
    
    $ignore_block = false;
    for ($i = 0; $i < count($arr); $i++)
    {
        if (strstr($arr[$i], '@media'))
            $ignore_block = true;
        elseif (strstr($arr[$i-1], '/*ends*/'))
            $ignore_block = false;
    
        if ($ignore_block == false)
        {
            $arr[$i] = str_replace("{", "{\n\t", $arr[$i]);
            $arr[$i] = str_replace("}", "\n}\n", $arr[$i]);
        }
    }
    file_put_contents("test.txt", implode("\n", $arr));
    

    Regex won’t work since lookbehind doesn’t allow variable length. Try and loop through the file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working off a relatively clean OS X install. I had Xcode, but removed
So, I'm working with swing and I need to find a clean (non-CPU-hogging-way) to
I already got the form validation working, I just need to apply a CSS
I consistently run into the need to clean up css files and strip unused
I had a clean working directory and brought in a clone from a Git
Working on a rather small, and simple layout, I decided to use Meyer's CSS
I couldn't seem to get my CSS working properly in IE (I'm using IE
I've got some CSS and HTML that I'm working on, I wanted to sub
I am working with some CSS that is poorly written to say the least.
When working with CSS inside of XML such as <span class=IwuvAS3></span> when parsed in

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.