日期:2014-05-17 浏览次数:20493 次
html='<table>'; var i,size,result; url='stock.php'; $.post(url, function(data){ result = eval('(' + data + ')'); size = result.length; for (i = 1; i < size; i++) { html += "<tr><td>" + result[i][1] + "</td><td>" + result[i][2]+ "</td><td>" + result[i][3] + "</td></tr>"; } }); html+='</table>'; alert(html);
<?php $url = 'http://data.stock.hexun.com/quotes/stock_4.htm'; $content = @file_get_contents($url); preg_match('/<table width=\"100%\"[^>]*>(.*)<\/table>/isU', $content,$str); preg_match_all('/<tr>(.*)<\/tr>/isU', $str[0],$strs); foreach ($strs[0] as $value){ //$pingyings = str_replace('__', '', $pingying); preg_match_all('/(<td[^>]*>|<th[^>]*>)(.*)(<\/td>|<\/th>|<\/div>)/isU', $value,$strs); $zhishu[] = $strs[2]; } for($i=1,$size=count($zhishu); $i<$size; $i++){ $zhishu[$i][0] = str_replace('<div>', '', $zhishu[$i][0]); //$zhishu[$i][0] = iconv('gbk', 'utf-8', $zhishu[$i][0]); } die(JSON($zhishu)); function arrayRecursive(&$array, $function, $apply_to_keys_also = false) { foreach ($array as $key => $value) { if (is_array($value)) { arrayRecursive($array[$key], $function, $apply_to_keys_also); } else { $array[$key] = $function($value); } if ($apply_to_keys_also && is_string($key)) { $new_key = $function($key); if ($new_key != $key) { $array[$new_key] = $array[$key]; unset($array[$key]); } } } } function JSON($array) { arrayRecursive($array, 'urlencode', true); $json = json_encode($array); return urldecode($json); }
<?php header("Content-type:text/html;charset=gbk"); $url = 'http://data.stock.hexun.com/quotes/stock_4.htm'; ... ... ... ?>
------解决方案--------------------
按你的数据,只能是一维数组
你的循环错误在于
1、起点错了,数组下标从 0 开始,你却从 1
2、明明是一维数组,你却当做二维来处理,自然就文不对题了
数据是三个一组的
for (i = 0; i < size; i+=3) {
html += "<tr><td>" + result[i] + "</td><td>" + result[i+1]+ "</td><td>" + result[i+2] + "</td></tr>";
}
------解决方案--------------------
用php组成2维的不就行了