Script issue moving to php 7.2 - Resolved

I am trying to get my site off of php 5.6 and move to 7.2. My JPGraph program needed to be upgraded which I have done and now at 4.2.6. The script below runs on php 5.6 perfectly now but will not run on php 7.2. The jpgraph 4.2.6 is supposed to support both.

Can someone look at the script below and tell me if you see anything that is depreciated etc. that would cause the graph to not display

Errors reported when domain is set to php7.2:

Fatal error: Uncaught TypeError: Argument 1 passed to JpGraphException::defaultHandler() must be an instance of Exception, instance of Error given in /home/auburnwe/public_html/jp/src/jpgraph_errhandler.inc.php:158 Stack trace: #0 [internal function]: JpGraphException::defaultHandler(Object(Error)) #1 {main} thrown in /home/auburnwe/public_html/jp/src/jpgraph_errhandler.inc.php on line 158

Blockquote

<?php
//where are your clientraw*-files relative to where this file is
$hostloc = "./";
date_default_timezone_set('America/Halifax');
include ("jp/src/jpgraph.php");           //maybe you have to change this path
include ("jp/src/jpgraph_line.php");       //maybe you have to change this path

// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);


// CtoF: converts degrees Celcius to degress Farenheight
function CtoF(&$value) {
  return round($value = ((1.8* $value) + 32),1);
} // end function C_to_F

// Create the graph and specify the scale for Y-axis
$graph = new Graph(400,250,"auto",30);    
$graph->SetScale("textlin");
$graph->SetShadow();
$graph->SetMarginColor("#191970");
$graph->img->SetAntiAliasing();

$url = "http://www.auburnweatherlive.com/atticdatapull.php";   // this is the page where you get the information
$str = file_get_contents($url);                                // retrieve the content of the page. This results in one single string with all the content

$str = ereg_replace(" <br/>","","$str");
$str = ereg_replace("X_Axis = ","","$str");                   
$str = ereg_replace("Y_Axis = ","xx","$str");

$data = explode("xx", $str);                                   // xx is the separator. You will get two strings in $data
$x = $data[0];                                                 // first part of $data is the X_Axis data
$y = $data[1];                                                 // second part of $data is the Y_Axis data

$datat = explode(" ", $x);                                     // make an array of $x 
$ydata = explode(" ", $y);                                     // make an array of $y

//create timearray for the x-axis
$n=1;
$limit=26;
putenv('TZ=America/Halifax');
$time=date(" H",time());
$datat=array($time);
$x=$datat;
//what you have to add
$add=3600;
$result=strtotime(now);
for ($n=1;$n++;)
{
$result=($result-$add);
$new=date('ga', $result);
$next=array("$new");
$datat= array_merge ($next,$datat);
if ($n>=$limit) break;
  }
$datat=array_merge($datat,$x);

// =====================================================================================
// From here we start creating the graphs
// =====================================================================================

// Adjust the margin
$graph->img->SetMargin(40,40,20,65);

// Create the two linear plot
$lineplot=new LinePlot($ydata);

// Add the plot to the graph
$graph->Add($lineplot);

//titles
$graph->title->Set("Attic");
$graph->title->SetFont(FF_FONT1,FS_BOLD,10);
$graph->title->SetColor("royalblue4");
//$graph->xaxis->title->Set("X-title");
//$graph->yaxis->title->Set("Y-title");

//x-axis
//$graph->xaxis->title->Set("X-title");
$time=date("M j",time());
$graph->xaxis->title->Set($time);
$graph->xaxis->title->SetColor(royalblue4);
$graph->xaxis->SetFont(FF_FONT1,FS_NORMAL,4);
$graph->xaxis->SetTickLabels($datat); 
$graph->xaxis->SetTextLabelInterval(3);
$graph->xaxis->SetPos("min"); 
$graph->xaxis->SetColor("royalblue4"); 
$graph->xaxis->HideTicks(true,true); 
$graph->xgrid->Show(true);

//y-axis
$graph->yaxis->SetColor("royalblue4");
$graph->yaxis->SetLabelFormat('%0.0f°F');
$graph->yaxis->scale->SetGrace(5);
$graph->yaxis->HideTicks(true,true); 


// Set the colors for the plots
$lineplot->SetColor("red");


// Set the legends for the plots
$lineplot->SetLegend("Temperature");


// Adjust the legend position
$graph->legend->SetLayout(LEGEND_HOR);
$graph->legend->Pos(0.5,0.9,"center","center");


// copy from here

$txt2=new Text("Current Temp"); 
$txt2->SetFont(FF_VERDANA, FS_BOLD,12); 
$txt2->SetPos(0.65,0.25,'right');
$txt2->SetColor("royalblue4"); 
$graph->AddText($txt2); 

// Get current clientraw reading
$clientraw = get_raw("${hostloc}clientraw.txt");
//$clientrawextra = get_raw("${hostloc}clientrawextra.txt");
//$clientrawdaily = get_raw ("${hostloc}clientrawdaily.txt");

//read the clientraw*-files we need
function get_raw( $rawfile ) {
$rawdata = array();
$fd = fopen($rawfile, "r");
if ($fd) {
$rawcontents = '';
while (! feof ($fd) ) {
$rawcontents .= fread($fd, 8192);
}
fclose($fd);
$delimiter = " ";
$rawdata = explode ($delimiter, $rawcontents);
} else {
$rawdata[0]= -9999;
}
return $rawdata;
}

//temp conversion===Extra temp #2 position 21======================================================================
$z=($clientraw['21']);

//$fahrenheit = $z * (212 - 32) / 100 + 32;
$fahrenheit = ($z * 1.8) + 32;
$num = $fahrenheit; 
$cut = substr($num, 0, ((strpos($num, '.')+1)+1)); 
$cut = $cut . "&#38;#0176;F";
// Cut the string from first character to a length of 2 past the decimal. 
// substr(cut what, start, ( (find position of decimal)+decimal itself)+spaces after decimal) ) 

$txt42=new Text("$cut"); 
$txt42->SetFont(FF_VERDANA, FS_BOLD,18); 
$txt42->SetPos(0.63,0.35,'right');
$txt42->SetColor("royalblue4"); 
$graph->AddText($txt42); 

// Display the graph
$graph->Stroke();

?>

try wrapping the whole thing in a try/catch block, then the “Uncaught TypeError” might get caught and php will shut up about it.

Resolved from another forum.

It is your use of ereg_replace that is causing the failure. It was deprecated in PHP 5.3 and removed in PHP 7.0. preg_replace is the recommend alternative. Based on what those statements are doing though you may be able to use str_replace instead of preg_replace.

For completeness a single call to str_replace should be able to replace those ereg_replace calls if I’m reading your code correctly.

$str = str_replace([’
',‘X_Axis = ‘,‘Y_Axis = ‘],[’’,’’,‘xx’],$str);

Sponsor our Newsletter | Privacy Policy | Terms of Service