views:

73

answers:

2

How can I parse the flash vars from a string like this?

<script type="text/javascript">
flashvars.added = "2010-07-18";
flashvars.name  = "testing+purposes";
flashvars.user  = "jhon+doe";
</script>

I am using curl to get the string.

+2  A: 

May not be perfect, but should at least get you started:

$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];
    }
}

var_dump($flashVars);

Probably could be more efficient, but again should help you get started.

Brad F Jacobs
it works! thanks mate.
greenbandit
A: 

Given the string in the question as $xml:

$dom = new DOMDocument();
$dom->loadXML($xml);
$arr = parse_ini_string($dom->textContent);
print_r($arr);

Output:

Array (
    [flashvars.added] => 2010-07-18
    [flashvars.name] => testing+purposes
    [flashvars.user] => jhon+doe
)
GZipp