fork(1) download
  1. <?php
  2.  
  3. $str = '<link rel="shortcut icon" href="http://localhost/teste/icon1.png">
  4. <link rel="shortcut icon" href="http://localhost/teste/icon2.png">
  5. <link rel="shortcut icon" href="http://localhost/teste/icon3.png">
  6. <link rel="shortcut icon" href="http://localhost/teste/icon4.png">';
  7.  
  8. preg_match_all( '/<link\s*(\w+=".*?")>/', $str, $matches );
  9.  
  10. function transpose( $array ) {
  11.  
  12. array_unshift( $array, null );
  13.  
  14. return call_user_func_array( 'array_map', $array );
  15. }
  16.  
  17. $matches = array_map(
  18.  
  19. function( $current ) {
  20.  
  21. $attributes = preg_split(
  22.  
  23. '/\s*(\w+)="(.*?)\s*"/',
  24.  
  25. $current, NULL, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
  26. );
  27.  
  28. $transposed = transpose( array_chunk( $attributes, 2 ) );
  29.  
  30. return array_combine( $transposed[ 0 ], $transposed[ 1 ] );
  31. },
  32.  
  33. $matches[ 1 ]
  34. );
  35.  
  36. var_dump( $matches );
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
array(4) {
  [0]=>
  array(2) {
    ["rel"]=>
    string(13) "shortcut icon"
    ["href"]=>
    string(32) "http://localhost/teste/icon1.png"
  }
  [1]=>
  array(2) {
    ["rel"]=>
    string(13) "shortcut icon"
    ["href"]=>
    string(32) "http://localhost/teste/icon2.png"
  }
  [2]=>
  array(2) {
    ["rel"]=>
    string(13) "shortcut icon"
    ["href"]=>
    string(32) "http://localhost/teste/icon3.png"
  }
  [3]=>
  array(2) {
    ["rel"]=>
    string(13) "shortcut icon"
    ["href"]=>
    string(32) "http://localhost/teste/icon4.png"
  }
}