php - How can I parse flashvars from string

233

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.

157

Answer

Solution:

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.

263

Answer

Solution:

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
)

People are also looking for solutions to the problem: php - Image gallery with codeigniter

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.