用php dom获取属性值
我尝试获取一些attiributue值。但是没有机会。下面你可以看到我的代码和解释。如何获得持续时间,文件等值?用php dom获取属性值
$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;
}
以下的输出:
{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
}
如何获得持续时间,文件等。值?
什么
$parsed = json_decode($the_value, true);
$duration = $parsed['duration'];
编辑: 由于json_decode()需要正确的JSON格式(键名和值必须用双引号),我们应该修复原始格式为正确的。因此,这里是代码:
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);
功能my_json_decode从this answer拍摄,略作修改。
你是不是想用'json_decode()'来代替? – 2012-05-10 20:15:43
尝试过,但输出为空 –
您可以替换'echo $ the_value;' 'var_dump($ the_value);出口();'并在这里粘贴输出? – itsmeee
你是什么意思“如何获得持续时间”。看起来你在这里'期间:'24',' –
想要'24'。 –