drisate Posted September 22, 2013 Share Posted September 22, 2013 Hey guys i need to capture the flash vars located in the middle of a string that contains the HTML of an external page. The string to capture looks like this: [...] var flashvars = { 'file' : 'http://***pods/lawandsociety/P130631MLJMuñiz-Fraticelli.mp3' 'width' : '492', 'height' : '60', 'controlbar' : 'bottom', 'dock' : 'false', 'icons' : 'false', 'logo.hide' : 'false', 'logo.position' : 'bottom-left', 'playlist' : 'none', 'skin' : 'http://***/wp-content/plugins/flash-video-player/skins/overlay/overlay.swf' 'autostart' : 'false', 'bufferlength' : '1', 'item' : '0', 'mute' : 'false', 'repeat' : 'none', 'shuffle' : 'false', 'smoothing' : 'true', 'stretching' : 'uniform', 'volume' : '90'}; [...] I need it to be converted into $flashvars[file] Quote Link to comment Share on other sites More sharing options...
drisate Posted September 22, 2013 Author Share Posted September 22, 2013 (edited) This is what i got so fare: $script = file_get_contents($link); $matches = array(); preg_match_all('~flashvars\.([a-z]+) .*=.*"(.*)";~i', $script, $matches); if (!empty($matches[1])) { $flashVars = array(); foreach ($matches[1] as $index => $key) { $flashVars[$key] = $matches[2][$index]; } } All i need is a valide regex Edited September 22, 2013 by drisate Quote Link to comment Share on other sites More sharing options...
.josh Posted September 22, 2013 Share Posted September 22, 2013 regex isn't really good for trying to parse code. Having said that, you're going to have to make some assumptions and hope for the best no matter how you slice this, short of running the page through something that can properly render the javascript. But here's my take: preg_match('~var flashvars\s*=\s*(\{.*?\});~s',$content,$flashvars); $flashvars = str_replace("'",'"',utf8_encode($flashvars[1])); $flashvars = array_map('utf8_decode',json_decode($flashvars,true)); This should (again, taking some liberties and hoping for the best), convert it into a php associative array. Array ( [file] => http://***pods/lawandsociety/P130631MLJMuñiz-Fraticelli.mp3 [width] => 492 [height] => 60 [controlbar] => bottom [dock] => false [icons] => false [logo.hide] => false [logo.position] => bottom-left [playlist] => none [skin] => http://***/wp-content/plugins/flash-video-player/skins/overlay/overlay.swf [autostart] => false [bufferlength] => 1 [item] => 0 [mute] => false [repeat] => none [shuffle] => false [smoothing] => true [stretching] => uniform [volume] => 90 ) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.