在PHP中提取文件并在图表中显示内容

时间:2014-02-19 17:15:23

标签: php function pie-chart text-extraction

我有一个名为“test.txt”的文件,其中包含以下内容:

BEGIN_SESSION 7
1h+ 47
30mn-1h 20
15mn-30mn 16
5mn-15mn 43
2mn-5mn 29
30s-2mn 35
0s-30s 170
END_SESSION

感谢用户wumm,它帮助我找到了一种提取数据并显示它的方法。所以问题是:我已经看到了这个在饼图中显示数据的功能:

function awstats_extract_session($session)
{
# Session range - Number of visits

$session = explode("\n", $session) ; 
unset($session[(count($session)-1)]) ; 
unset($session[0]) ; 
$sessions = array();
foreach ($session as $key => $value) {
    $session[$key] = explode(" ", $value) ;
    //print_array($session[$key]);
    $sessions[] = array($session[$key][0],$session[$key][1]) ; 
}
$sessions = json_encode($sessions, JSON_NUMERIC_CHECK) ; 


?>
<div id="collapse_awstats_extract_session" class="accordion-body collapse in">
    <div class="accordion-inner">
        <div id="chart-session" style="height:350px; width:700px; margin:0 auto;"></div>
    </div>
</div>
<script>
    jQuery(document).ready(function(){
      var data = <?php echo $sessions; ?>;
      var plot1 = jQuery.jqplot('chart-session', [data], {
        title: 'Durée des visites',
        seriesDefaults: {
          renderer: jQuery.jqplot.PieRenderer, 
          rendererOptions: {
            showDataLabels: true,
          },
        }, 
        legend: { show:true, location: 'e' },           
      });
    });
</script>

还有一个函数调用前一个函数:

 <?php
            echo '<div class="accordion-group">';

            preg_match("/BEGIN_SESSION(.*)END_SESSION/is", $awstats, $matches);
            $session = $matches[0];
            preg_match('/30s-2mn ([0-9]{2})/ms', $session, $matches); 
            $a = $matches[1];
            preg_match('/0s-30s ([0-9]{3})/ms', $session, $matches); 
            $b = $matches[1];

            var_dump($a+$b);



            awstats_title("<a class='accordion-toggle' data-toggle='collapse' data-parent='#accordion_awstats_navigation' href='#collapse_awstats_extract_session'><span class='glyphicon glyphicon-collapse-down'></span> Durée des visites</a>", "session") ;
            awstats_extract_session($session) ;

我希望显示所有内容,但最后两个“30s-2mn 35”“0s-30s 170” 我想将它显示为“0s-2mn 205”。请帮助 P.S: 或者有没有办法将文件“test.txt”更改为“test2.txt”:

BEGIN_SESSION 7
1h+ 47
30mn-1h 20
15mn-30mn 16
5mn-15mn 43
2mn-5mn 29
0s-2mn 205
END_SESSION

谢谢!

1 个答案:

答案 0 :(得分:0)

希望这有帮助。

preg_match("/BEGIN_SESSION(.*)END_SESSION/is", $test, $matches);
$session = $matches[0];
preg_match('/30s-2mn ([0-9]{2})/ms', $session, $matches); 
$a = $matches[1];
preg_match('/0s-30s ([0-9]{3})/ms', $session, $matches); 
$b = $matches[1];

$session = preg_replace('/30s-2mn ([0-9]{2})\n0s-30s ([0-9]{3})/ms', "0s-2mn " . ($a+$b), $session);

然后$session包含您在问题的最后一个代码示例中显示的文本。要写出来,你可以做到。

$session现在包含:

BEGIN_SESSION 7
1h+ 47
30mn-1h 20
15mn-30mn 16
5mn-15mn 43
2mn-5mn 29
0s-2mn 205
END_SESSION