0

I have a css file with this content:

#firstdiv { width:100%; height:200px; }
#seconddiv { width:80%; height:70px; }
#thirddiv { width:80%; height:70px; }
#firstdiv { color:red; background:yellow; }
#seconddiv {  color:red; background:green; }
#firstdiv { border:3px solid black; border-rdius:5px; }

How can I remove all #firstdiv css properties using php?

This is my desired output:

#seconddiv { width:80%; height:70px; }
#thirddiv { width:80%; height:70px; }
#seconddiv {  color:red; background:green; }
Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
  • 1
    What have you already tried? – MJH Dec 30 '16 at 17:09
  • if it's not a huge file, just read the file as string, make the changes, and overwrite the file at the end. – xRahul Dec 30 '16 at 17:10
  • i have tried this http://stackoverflow.com/questions/5712878/how-to-delete-a-line-from-the-file-with-php – user7358605 Dec 30 '16 at 17:10
  • use `file()` function to read whole file into array and the apply a loop with `strpos()` and removing the corresponding array value with `unset()`. After that write the remaining array into file once again – Alive to die - Anant Dec 30 '16 at 17:11
  • i have tried this http://stackoverflow.com/questions/5712878/how-to-delete-a-line-from-the-file-with-php but it removes only "#firstdiv {" but i want to remove all css properties of that div in this huge css file – user7358605 Dec 30 '16 at 17:12

1 Answers1

0

The most easy way would to split the file based on newlines and then check for each row whether it starts with the string you don't want and finally save the file to the location.

$filelocation = "/path/to/file"; //please update for your situation
$csscontents = file_get_contents($filelocation);
$lines = explode(PHP_EOL,$csscontents);
$csscontents = '';
foreach($lines as $line) {
    if (substr($line,0,9) !== "#firstdiv") $csscontents .= $line . PHP_EOL;
}
file_put_contents($filelocation,$csscontents);

In case there are multiple selectors on one line, you need to do this

$filelocation = "/path/to/file"; //please update for your situation
$csscontents = file_get_contents($filelocation);
$lines = explode('}',$csscontents);
$csscontents = '';
foreach($lines as $line) {
    if (substr(preg_replace('/\s+/', '',$line),0,9) !== "#firstdiv" AND !empty($line) AND !ctype_space($line)) $csscontents .= $line . "}";
}
file_put_contents($filelocation,$csscontents);
rrr
  • 412
  • 4
  • 10
  • Thanks, Its working fine. but what if i write #firstdiv {} and #seconddiv {} css properties in same line.. like – user7358605 Dec 30 '16 at 17:29
  • Could you please specify your question. Do you mean you're having the whole file as one line or do you mean you've got a css file where sometimes you do have `#firstdiv #seconddiv {}` as one line? – rrr Dec 30 '16 at 17:41
  • sorry, my css file is like this #firstdiv { some properties} #seconddiv { some properties } #thirddiv {some properties} all these in same line and in next line #firstdiv { some other properties} #seconddiv { some other properties } #thirddiv {some other properties} and i want to remove only #firstdiv properties – user7358605 Dec 30 '16 at 17:45
  • Thanks for your reply.. i found 2 errors with your code (1st) after executing this code all the '#firstdiv' css properties are removed that's fine but am getting one new "#" in the css file (i know it don't effect my css properties but just to notify you) (2nd) I have one css property as 'border-radius:3px solid #333333;' in this <- "#" is detected as a new 'id' selector. please see the image in this link https://postimg.org/image/wfps5og9r/ – user7358605 Dec 30 '16 at 19:05
  • Thanks for everything – user7358605 Dec 30 '16 at 19:40
  • If my answer helped you, would you mind accepting it as answer? – rrr Dec 30 '16 at 20:31