是否可以使用DOM Document来提取javascript值?
<script type="text/javascript">
var datePickerDate = "October 14, 2014";
$(document).ready(function(){
// meetings datepicker
$("#datepicker").datepicker({
showOn: 'button',
buttonImage: '/images/calendar.gif',
buttonImageOnly: true,
onSelect: function(dateText, inst) {
populate_meetings(dateText, 0)
},
dateFormat: 'd MM yy',
minDate: new Date(2009, 0, 1), maxDate: 0
});
$('#datepicker').datepicker('option', 'defaultDate', new Date("October 14, 2014"));
$('#datepicker').val("October 14, 2014");
});
//call on page load to get meetings and date
populate_meetings("October 14, 2014", 15515);
</script>
这是在HTML中输出所以我知道它在那里...我想读取日期所以pull var datePickerDate =所以它返回2014年10月14日
- 更多澄清---
我正在使用domdocument抓取一个网站并抓住这样的HTML
$html = file_get_contents($url);
//set the main page html code that will be resused
$dom = new DOMDocument();
// load html
@$dom->loadHTML($html);
在html中是一些javascript,它有一个日期....我想抓住那个日期并坚持在一个可靠的
填充会议只会填充日期
的下拉框答案 0 :(得分:0)
你不能使用PHP执行javascript(显然),所以最好的方法是使用正则表达式获取JS变量。以下是使用DOMXPath执行此操作的示例:
# create a new XPath object that will operate on the DOM that you've initialised
# in your code
$xp = new DOMXPath($dom);
# find all the script elements in the page
$scripts = $xp->query("//script");
foreach ($scripts as $s) {
# see if there are any matches for var datePickerDate in the script node's contents
if (preg_match('#var datePickerDate = "(.*?)"#', $s->nodeValue, $matches)) {
# the date itself (captured in brackets) is in $matches[1]
print_r($matches[1]);
}
}
输出:
October 14, 2014