0

I try to get some attiributue values. But have no chance. Below yo can see my code and explanation. How to get duration, file etc.. values?

$url="http://www.some-url.ltd";

    $dom = new DOMDocument;
    @$dom->loadHTMLFile($url);
    $xpath = new DOMXPath($dom);
    $the_div = $xpath->query('//div[@id="the_id"]');
    foreach ($the_div as $rval) {
        $the_value = trim($rval->getAttribute('title'));
        echo $the_value;
    }

The output below:

{title:'title',
                description:'description',
                scale:'fit',keywords:'',
                file:'http://xxx.ccc.net/ht/2012/05/10/419EE45F98CD63F88F52CE6260B9E85E_c.mp4',
                type:'flv',
                duration:'24',
                screenshot:'http://xxx.ccc.net/video/2012/05/10/419EE45F98CD63F88F52CE6260B9E85E.jpg?v=1336662169',
                suggestion_path:'/videoxml/player_xml/61319',
                showSuggestions:true,
                autoStart:true,
                width:412,
                height:340,
                autoscreenshot:true,
                showEmbedCode:true,
                category: 1,
                showLogo:true
                                                }

How to get duration, file etc.. values?

hakre
  • 193,403
  • 52
  • 435
  • 836
dr.linux
  • 752
  • 5
  • 15
  • 37

1 Answers1

2

What about

$parsed = json_decode($the_value, true);
$duration = $parsed['duration'];

EDIT: Since json_decode() requires proper JSON formatting (key names and values must be enclosed in double quotes), we should fix original formatting into the correct one. So here is the code:

function my_json_decode($s, $associative = false) {
$s = str_replace(array('"', "'", 'http://'), array('\"', '"', 'http//'), $s);
        $s = preg_replace('/(\w+):/i', '"\1":', $s);
        $s = str_replace('http//', 'http://', $s);
        return json_decode($s, $associative);
}

$parsed = my_json_decode($var, true);

Function my_json_decode is taken from this answer, slightly modified.

Community
  • 1
  • 1
itsmeee
  • 1,627
  • 11
  • 12
  • can you replace 'echo $the_value;' with 'var_dump($the_value); exit();' and paste the output here? – itsmeee May 10 '12 at 20:29
  • the problem is that json_decode() requires json to be properly formatted, which means double quotes around keys and values... So my_json_decode() function formats json into the right one. I used the answer to this question partly: http://stackoverflow.com/questions/1575198/invalid-json-parsing-using-php/1575315#1575315 – itsmeee May 10 '12 at 21:29