arrays - php explode doesn't second item -
i'm trying split image string: $output = "<img typeof="foaf:image" src="http://asite.dev/sites/default/files/video/mbi_part%201_v9.jpg" width="1920" height="1080" alt="" />
i'm doing this: $split = explode('"', $output);
but when print_r($split);
it returns:
array ( [0] => typeof="foaf:image" [2] => src="http://makingitcount.dev/sites/default/files/video/mbi_part%201_v9.jpg" [3] => width="1920" [4] => height="1080" [5] => alt="" [6] => /> )
no second value! where'd go? split[1]
throws error, of course. notice "<img
" part of string isn't in array either.
the problem stems parsing of html tag. if remove <img
@ beginning of html string, you'll notice rest of attributes parse array proper number sequence (including '1' element). can solve problem formatting quotes tell php not parse html , treat entire unit strictly string.
if want bypass whole mess, can use regular expression matching collect tag information , pass array. $matches[0][*] contain of tag attributes, , $matches[1] contains tag (img)
$output = '<img typeof="image" src="http://asite.dev/sites/default/files/video/mbi_part%201_v9.jpg" width="1920" height="1080" alt="" />'; $pattern = '( \w+|".*?")'; preg_match_all($pattern, $output, $matches); preg_match("[\w+]",$output,$matches[1]); print_r($matches);
which gives
array ( [0] => array ( [0] => typeof [1] => "image" [2] => src [3] => "http://asite.dev/sites/default/files/video/mbi_part%201_v9.jpg" [4] => width [5] => "1920" [6] => height [7] => "1080" [8] => alt [9] => "" ) [1] => array ( [0] => img ) )
Comments
Post a Comment