在PHP中提取文件并在图表中显示内容 [英] Extract file in PHP and display content in chart

查看:88
本文介绍了在PHP中提取文件并在图表中显示内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为test.txt的文件,其内容如下:

  BEGIN_SESSION 7 
1小时+ 47小时b $小时3000-1000小时20小时b小时1500-1000小时16小时b小时500-1,500小时43小时b小时2小时-500小时29小时b小时30-20小时35小时b小时0小时-30小时170
END_SESSION

感谢用户 wumm 它帮助我找到一种方法来提取数据并将其显示出来。所以问题是:我已经见证了在饼图中显示数据的这个函数: p>

 函数awstats_extract_session($ session)
{
#会话范围 - 访问次数

$ session = explode(\\\
,$ session);
unset($ session [(count($ session)-1)]);
unset($ session [0]);
$ sessions = array();
foreach($ session为$ 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_sessionclass =accordion-body collapse in>
< div class =accordion-inner>
< div id =chart-sessionstyle =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éedes visites',
seriesDefaults:{
renderer:jQuery.jqplot.PieRenderer,
rendererOptions:{
showDataLabels:真实的,
},
},
图例:{show:true,location:'e'},
});
});
< / script>

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

 <?php 
echo'< div class =accordion-group>';
$ b 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>< span class ='glyphicon glyphicon-collapse-down'>< / span>可持续发展< / a>,会话
awstats_extract_session($ session);

我希望显示所有内容,但最后两个30s-2mn 350s -30s 170
我想将其设置为0s-2mn 205。请帮助
PS:
或者有没有办法将文件test.txt更改为

  BEGIN_SESSION 7 
1h + 47
30mn-1h 20
1,500万-3,000万16
500-1,500万43
2mn-5mn 29
0s-2mn 205
END_SESSION

谢谢!

解决方案


$ b $

  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})\\\
0s-30s([0-9] {3})/ ms', 2mn($ a + $ b),$ session);

然后 $ session 包含您显示的文字在你的问题的最后一个代码示例中。


$ b

$ session 现在包含:

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


I have this a file named "test.txt" which has the following content:

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

And thanks to the user wumm it help me to find a way to extract the data and display it.So the problem is :I've witten this function that display data in a pie chart :

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>

And there's a function that calls that previous one :

 <?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) ;

And i would like that display all it content but the last two "30s-2mn 35" "0s-30s 170" I'd like to diplay it as "0s-2mn 205".Please Help P.S: Or is there a way to change the file "test.txt" in such to have "test2.txt":

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

Thanks!

解决方案

Hope this helps.

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);

Then $session contains the text you showed in the last code example in your question. To write it out you could do.

$session now contains:

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

这篇关于在PHP中提取文件并在图表中显示内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆