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?

1
  • What do you mean "How to get duration". It seems you have it right here duration:'24', Commented May 10, 2012 at 20:05

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

can you replace 'echo $the_value;' with 'var_dump($the_value); exit();' and paste the output here?
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: stackoverflow.com/questions/1575198/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.