fork download
  1. <?php
  2.  
  3. $string = "Color: black; Weight: 2.3 kg; Screen size: 15.6''; Screen resolution: 1366 x 768;";
  4. $specs = array();
  5. preg_match_all('/(.*?):\s+(.*?); ?/i', $string, $matches, PREG_PATTERN_ORDER);
  6. for ($i = 0; $i < count($matches[1]); $i++) {
  7. $specs[$matches[1][$i]] = $matches[2][$i];
  8. }
  9. //cret
  10. function whatIsThe($item, $specs){
  11. return $specs[$item];
  12. }
  13.  
  14. echo whatIsThe("Color", $specs);
  15. //black
  16. echo whatIsThe("Screen resolution", $specs);
  17. //black
  18.  
  19. //print_r($specs);
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
black1366 x 768