fork download
  1. <?php
  2. //built with help from the amazing people on stackoverflow
  3. //http://stackoverflow.com/questions/17928427/regular-expression-to-not-match-something-in-quotes/17928889
  4.  
  5. /////////////////////////////////////////////////////////////////////
  6. $contents = 'img[style*="float: left"]
  7. img: [style*="float: left"]
  8. img( [style*="float: left"]';
  9. echo drupal_load_stylesheet_content($contents, TRUE);
  10. ////////////////////////////////////////////////////////////////////
  11.  
  12.  
  13. function drupal_load_stylesheet_content($contents, $optimize = FALSE) {
  14. // Remove multiple charset declarations for standards compliance (and fixing Safari problems).
  15. $contents = preg_replace('/^@charset\s+[\'"](\S*)\b[\'"];/i', '', $contents);
  16.  
  17. if ($optimize) {
  18. // Perform some safe CSS optimizations.
  19. // Regexp to match comment blocks.
  20. $comment = '/\*[^*]*\*+(?:[^/*][^*]*\*+)*/';
  21. // Regexp to match double quoted strings.
  22. $double_quot = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"';
  23. // Regexp to match single quoted strings.
  24. $single_quot = "'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'";
  25. // Strip all comment blocks, but keep double/single quoted strings.
  26. $contents = preg_replace(
  27. "<($double_quot|$single_quot)|$comment>Ss",
  28. "$1",
  29. $contents
  30. );
  31. // Remove certain whitespace.
  32. // There are different conditions for removing leading and trailing
  33. // whitespace.
  34. // @see http://p...content-available-to-author-only...p.net/manual/en/regexp.reference.subpatterns.php
  35. $contents = preg_replace('<
  36. # Strip leading and trailing whitespace.
  37. \s*([@{};,])\s*
  38. # Strip only leading whitespace from:
  39. # - Closing parenthesis: Retain "@media (bar) and foo".
  40. | \s+([\)])
  41. # Strip only trailing whitespace from:
  42. # - Opening parenthesis: Retain "@media (bar) and foo".
  43. | ([\(])\s+
  44. >xS',
  45. // Only one of the three capturing groups will match, so its reference
  46. // will contain the wanted value and the references for the
  47. // two non-matching groups will be replaced with empty strings.
  48. '$1$2$3',
  49. $contents
  50. );
  51.  
  52. //Strip only trailing whitespace from colons unless that colon appears within double quotes
  53. $contents = preg_replace_callback('/"[^"]*"|([:])\s+/ims',
  54. function ($matches) {
  55. if (array_key_exists (1, $matches)) {
  56. return $matches[1] ;
  57. }
  58. return $matches[0];
  59. },
  60. $contents
  61. );
  62.  
  63. // End the file with a new line.
  64. $contents = trim($contents);
  65. $contents .= "\n";
  66. }
  67.  
  68. // Replaces @import commands with the actual stylesheet content.
  69. // This happens recursively but omits external files.
  70. $contents = preg_replace_callback('/@import\s*(?:url\(\s*)?[\'"]?(?![a-z]+:)([^\'"\()]+)[\'"]?\s*\)?\s*;/', '_drupal_load_stylesheet', $contents);
  71. return $contents;
  72. }
  73.  
  74.  
  75.  
  76.  
  77.  
  78. ?>
  79.  
Success #stdin #stdout #stderr 0.01s 20568KB
stdin
Standard input is empty
stdout
img[style*="float: left"]
img:[style*="float: left"]
img([style*="float: left"]
stderr
PHP Warning:  preg_replace_callback(): Requires argument 2, '_drupal_load_stylesheet', to be a valid callback in /home/dm4eCV/prog.php on line 70