So I'm writing a PHP script that will read in a CSS file, then put the comments and actual CSS in separate arrays. The script will then build a page with the CSS and comments all nicely formatted.
The basic logic for the script is this:
- Read in a new line
- If it starts with a forward slash or ends with an opening bracket, set a bool for CSS or comments to true
- Add that line to the appropriate element in the appropriate array
- If the last character is a backslash (end of a comment) or the first character is a closing bracket (end of a CSS tag), set necessary bool to false
- Rinse, repeat
If someone sees an error in that, feel free to point it out, but I think it should do what I want.
The tricky part is the last if statement, checking if the last character is a backslash. Right now I have:
if ($line{(strlen($line) - 3)} == "\\") {do stuff}
where $line is the last line read in from the file. Not entirely sure why I have to go back 3 characters, but I'm guessing it's because there's a newline at the end of each string when reading it in from a file. However, this if statement is never true, even though there are definitely lines which end with slashes. This
echo "<br />str - 3: " . $line{(strlen($line)-3)};
even returns a backslash, yet the if statement is never trigged.