Using PHP how can I find a CSS tag/id/class and replace the style within the curly braces?

I need to traverse through a CSS file finding specific items and changing their CSS to updated stuff for a theme editor. I have a variable with all the CSS for between the curly braces, I just need to find a way to select the braces after the given tag and replace the contents. Any help would be greatly appreciated! Thanks

Answers


Maybe not the only solution, but have you thought about putting the CSS into the database and generate the CSS file on the fly?

Storing it in the database also gives you the possibility to re-generate the file every time somethings changed and have it cached like that to save performance.

To modify an existing file you probably need to traverse through the file and hope that nobody manually changes the style conventions for your CSS-file and replace as you go along by remembering the last tag/class/id expression like that (will probably not work out of the box!):

$newFileContent = "";
$searchClass = "td.content";
$lines = file("style.css");
$insideSearchedTag = false;
foreach($lines as $line) {
  if (strstr($line, $searchClass) !== false) {
    $insideSearchedTag = true;
    $newFileContent .= "\n" . $line;
  }
  else if (strstr($line, "}") !== false) {
    $insideSearchedTag = false;
    $newFileContent .= "\n" . $line;
  }
  else if ($insideSearchedTag) {
    // search/replace the content you want to replace.
    $newLineContent = str_ireplace($searchStyle, $replaceStyle, $line);
    $newFileContent .= "\n" . $newLineContent;
  }
  else { $newFileContent .= "\n" . $line; }
}
fwrite($file, $newFileContent);

Need Your Help

Running command in existing shell from gvim

linux shell vim ipc

It all starts from a shell. For example I am using urxvt with zsh. There I open some file with gvim. In this case it is a LaTeX file. Now I need to execute some command (for compiling the document,

Access Datalist label with JQuery or Javascript

javascript jquery asp.net datalist

I have a datalist generated from the back end. From the client side I need to access a label in the datalist with Jquery or Javascript. Here is the client side that's generated:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.