fork(3) download
  1. <?php
  2.  
  3. $str = '<div class="article"><img src="/images/image.png" alt="alt for image"></div>';
  4. $css = '<style> .article { font-size:16px; font-weight:bold; width:566px; height:576px; } </style>
  5. ';
  6.  
  7. function replace($str, $css) {
  8.  
  9. preg_match( '@>\s\.(.+)\s{\s(.*)\s}@', $css, $res);
  10.  
  11. $style['class'] = $res[1];
  12. $attrs = explode("; ", $res[2]);
  13.  
  14. foreach ($attrs as $attr) {
  15. if (strlen(trim($attr)) > 0) {
  16. $kv = explode(":", trim($attr));
  17. $style[trim($kv[0])] = trim($kv[1]);
  18. }
  19. }
  20.  
  21. $rep = '<div class="$1">
  22. <div style="background: transparent url($2) no-repeat;
  23. background-position: center center; background-size: cover; width: '.$style['width'].';
  24. height: '.$style['height'].'">$3</div>
  25. </div>
  26. ';
  27.  
  28. return preg_replace( '@.+class="(.+)"><.*="(.+)"\\salt="(.+)".+@', $rep, $str );
  29. }
  30. $str = replace($str, $css);
  31. ?>
  32.  
  33. <html>
  34. <head>
  35. <?php echo $css; ?>
  36. </head>
  37. <body>
  38. <?php echo $str; ?>
  39. </body>
  40. </html>
Success #stdin #stdout 0.03s 52480KB
stdin
Standard input is empty
stdout
<html>
<head>
<style> .article { font-size:16px; font-weight:bold; width:566px; height:576px; } </style>
</head>
<body>
<div class="article">
 <div style="background: transparent url(/images/image.png) no-repeat; 
 background-position: center center; background-size: cover; width: 566px;
 height: 576px;">alt for image</div>
</div>
</body>
</html>