问题
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!
回答1:
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
来源:https://stackoverflow.com/questions/21887606/extract-file-in-php-and-display-content-in-chart