fpdf custom font works but not on any other computer - php

I'm generating PDF invoices using fpdf and I would like to use a custom font. I got that working by converting the font and adding it to the fpdf and it works but if I open the PDF on any other computer the font is gone
the sample of converted font (OpenSans)
<?php
$type='TrueType';
$name='OpenSans';
$desc=array('Ascent'=>1069,'Descent'=>-293,'CapHeight'=>714,'Flags'=>32,'FontBBox'=>'[-550 -271 1204 1048]','ItalicAngle'=>0,'StemV'=>70,'MissingWidth'=>600);
$up=-75;
$ut=50;
$cw=array(
chr(0)=>600,chr(1)=>600,chr(2)=>600,chr(3)=>600,chr(4)=>600,chr(5)=>600,chr(6)=>600,chr(7)=>600,chr(8)=>600,chr(9)=>600,chr(10)=>600,chr(11)=>600,chr(12)=>600,chr(13)=>600,chr(14)=>600,chr(15)=>600,chr(16)=>600,chr(17)=>600,chr(18)=>600,chr(19)=>600,chr(20)=>600,chr(21)=>600,
chr(22)=>600,chr(23)=>600,chr(24)=>600,chr(25)=>600,chr(26)=>600,chr(27)=>600,chr(28)=>600,chr(29)=>600,chr(30)=>600,chr(31)=>600,' '=>260,'!'=>267,'"'=>401,'#'=>646,'$'=>572,'%'=>823,'&'=>730,'\''=>221,'('=>296,')'=>296,'*'=>552,'+'=>572,
','=>245,'-'=>322,'.'=>266,'/'=>367,'0'=>572,'1'=>572,'2'=>572,'3'=>572,'4'=>572,'5'=>572,'6'=>572,'7'=>572,'8'=>572,'9'=>572,':'=>266,';'=>266,'<'=>572,'='=>572,'>'=>572,'?'=>429,'#'=>899,'A'=>633,
'B'=>648,'C'=>631,'D'=>729,'E'=>556,'F'=>516,'G'=>728,'H'=>738,'I'=>279,'J'=>267,'K'=>614,'L'=>519,'M'=>903,'N'=>754,'O'=>779,'P'=>602,'Q'=>779,'R'=>618,'S'=>549,'T'=>553,'U'=>728,'V'=>595,'W'=>926,
'X'=>577,'Y'=>560,'Z'=>571,'['=>329,'\\'=>367,']'=>329,'^'=>542,'_'=>448,'`'=>577,'a'=>556,'b'=>613,'c'=>476,'d'=>613,'e'=>561,'f'=>339,'g'=>548,'h'=>614,'i'=>253,'j'=>253,'k'=>525,'l'=>253,'m'=>930,
'n'=>614,'o'=>604,'p'=>613,'q'=>613,'r'=>408,'s'=>477,'t'=>353,'u'=>614,'v'=>501,'w'=>778,'x'=>524,'y'=>504,'z'=>468,'{'=>379,'|'=>551,'}'=>379,'~'=>572,chr(127)=>600,chr(128)=>590,chr(129)=>600,chr(130)=>245,chr(131)=>600,
chr(132)=>405,chr(133)=>784,chr(134)=>502,chr(135)=>510,chr(136)=>600,chr(137)=>1202,chr(138)=>549,chr(139)=>304,chr(140)=>549,chr(141)=>553,chr(142)=>571,chr(143)=>571,chr(144)=>600,chr(145)=>170,chr(146)=>170,chr(147)=>350,chr(148)=>350,chr(149)=>376,chr(150)=>500,chr(151)=>1000,chr(152)=>600,chr(153)=>776,
chr(154)=>477,chr(155)=>304,chr(156)=>477,chr(157)=>353,chr(158)=>468,chr(159)=>468,chr(160)=>260,chr(161)=>592,chr(162)=>592,chr(163)=>523,chr(164)=>572,chr(165)=>633,chr(166)=>551,chr(167)=>516,chr(168)=>577,chr(169)=>832,chr(170)=>549,chr(171)=>497,chr(172)=>572,chr(173)=>322,chr(174)=>832,chr(175)=>571,
chr(176)=>428,chr(177)=>572,chr(178)=>197,chr(179)=>261,chr(180)=>577,chr(181)=>619,chr(182)=>655,chr(183)=>266,chr(184)=>227,chr(185)=>556,chr(186)=>477,chr(187)=>497,chr(188)=>519,chr(189)=>577,chr(190)=>253,chr(191)=>468,chr(192)=>618,chr(193)=>633,chr(194)=>633,chr(195)=>633,chr(196)=>633,chr(197)=>519,
chr(198)=>631,chr(199)=>631,chr(200)=>631,chr(201)=>556,chr(202)=>556,chr(203)=>556,chr(204)=>556,chr(205)=>279,chr(206)=>279,chr(207)=>729,chr(208)=>722,chr(209)=>754,chr(210)=>754,chr(211)=>779,chr(212)=>779,chr(213)=>779,chr(214)=>779,chr(215)=>572,chr(216)=>618,chr(217)=>728,chr(218)=>728,chr(219)=>728,
chr(220)=>728,chr(221)=>560,chr(222)=>553,chr(223)=>622,chr(224)=>408,chr(225)=>556,chr(226)=>556,chr(227)=>556,chr(228)=>556,chr(229)=>253,chr(230)=>476,chr(231)=>476,chr(232)=>476,chr(233)=>561,chr(234)=>561,chr(235)=>561,chr(236)=>561,chr(237)=>253,chr(238)=>253,chr(239)=>613,chr(240)=>613,chr(241)=>614,
chr(242)=>614,chr(243)=>604,chr(244)=>604,chr(245)=>604,chr(246)=>604,chr(247)=>572,chr(248)=>408,chr(249)=>614,chr(250)=>614,chr(251)=>614,chr(252)=>614,chr(253)=>504,chr(254)=>353,chr(255)=>253);
$enc='cp1250';
$diff='131 /.notdef 136 /.notdef 140 /Sacute /Tcaron 143 /Zacute 152 /.notdef 156 /sacute /tcaron 159 /zacute 161 /caron /breve /Lslash 165 /Aogonek 170 /Scedilla 175 /Zdotaccent 178 /ogonek /lslash 185 /aogonek /scedilla 188 /Lcaron /hungarumlaut /lcaron /zdotaccent /Racute 195 /Abreve 197 /Lacute /Cacute 200 /Ccaron 202 /Eogonek 204 /Ecaron 207 /Dcaron /Dcroat /Nacute /Ncaron 213 /Ohungarumlaut 216 /Rcaron /Uring 219 /Uhungarumlaut 222 /Tcedilla 224 /racute 227 /abreve 229 /lacute /cacute 232 /ccaron 234 /eogonek 236 /ecaron 239 /dcaron /dcroat /nacute /ncaron 245 /ohungarumlaut 248 /rcaron /uring 251 /uhungarumlaut 254 /tcedilla /dotaccent';
$originalsize=217360;
?>

Related

Contact form {"error":true,"message":"404 not found!"}

I got an error after sending contact form and the message doesn't come in my admin panel. Any ideas?
{"error":true,"message":"404 not found!"}
enter image description here
PHP version: 7.3.33
PrestaShop version: 1.7.8.8
Request URL: https://oneride.eu/en/contact-us
Request Method: POST
Status Code: 200
Request Form Data:
id_contact: 2
from: mail#mail.com
id_order: 708
fileUpload: (binary)
message: test
psgdpr_consent_checkbox: 1
url:
token: a3511a4e2dcb2717ab336e20b8ca69b3
submitMessage: Send
Debug mode //oneride.eu/en/contact-us :
(1/1) ContextErrorException
Warning: json_decode() expects parameter 1 to be string, array given
in tawkto.php line 135
at Tawkto->hookDisplayFooter(array('smarty' => object(SmartyCustomTemplate), 'cookie' => object(Cookie), 'cart' => object(Cart), 'altern' => 1))
in Hook.php line 1007
at HookCore::coreCallHook(object(Tawkto), 'hookDisplayFooter', array('smarty' => object(SmartyCustomTemplate), 'cookie' => object(Cookie), 'cart' => object(Cart), 'altern' => 1))
in Hook.php line 13
at Hook::coreCallHook(object(Tawkto), 'hookDisplayFooter', array('smarty' => object(SmartyCustomTemplate), 'cookie' => object(Cookie), 'cart' => object(Cart), 'altern' => 1))
in Hook.php line 431
at HookCore::callHookOn(object(Tawkto), 'displayFooter', array('smarty' => object(SmartyCustomTemplate), 'cookie' => object(Cookie), 'cart' => object(Cart), 'altern' => 1))
in Hook.php line 944
at HookCore::exec('displayFooter', array('smarty' => object(SmartyCustomTemplate), 'cookie' => object(Cookie), 'cart' => object(Cart), 'altern' => 1), null)
in smarty.config.inc.php line 167
at smartyHook(array('h' => 'displayFooter'), object(SmartyCustomTemplate))
in SmartyLazyRegister.php line 83
at SmartyLazyRegister->__call('smartyHook', array(array('h' => 'displayFooter'), object(SmartyCustomTemplate)))
in a7a56b2fa98f8bb9c3e9557be5244fafdabbb7d0_2.file.footer.tpl.php line 78
at Block_99635320563e3665eedae42_10577993->callBlock(object(SmartyCustomTemplate))
in smarty_internal_runtime_inheritance.php line 248
at Smarty_Internal_Runtime_Inheritance->callBlock(object(Block_99635320563e3665eedae42_10577993), object(SmartyCustomTemplate))
in smarty_internal_runtime_inheritance.php line 184
at Smarty_Internal_Runtime_Inheritance->process(object(SmartyCustomTemplate), object(Block_99635320563e3665eedae42_10577993))
in smarty_internal_runtime_inheritance.php line 156
at Smarty_Internal_Runtime_Inheritance->instanceBlock(object(SmartyCustomTemplate), 'Block_99635320563e3665eedae42_10577993', 'hook_footer')
in a7a56b2fa98f8bb9c3e9557be5244fafdabbb7d0_2.file.footer.tpl.php line 30
at content_63e3665eee0747_13776542(object(SmartyCustomTemplate))
in smarty_template_resource_base.php line 123
at Smarty_Template_Resource_Base->getRenderedTemplateCode(object(SmartyCustomTemplate))
in smarty_template_compiled.php line 114
at Smarty_Template_Compiled->render(object(SmartyCustomTemplate))
in smarty_internal_template.php line 216
at Smarty_Internal_Template->render()
in smarty_internal_template.php line 385
at Smarty_Internal_Template->_subTemplateRender('file:_partials/footer.tpl', null, 'at_moviclayouts/layout-left-column.tpl', 0, 31536000, array(), 0, false)
in e2edce3fe10e6aa05aff7c1599af26310b1ccbc7_2.file.layout-both-columns.tpl.php line 358
at Block_19810866863e3665eb94015_33088710->callBlock(object(SmartyCustomTemplate))
in smarty_internal_runtime_inheritance.php line 248
at Smarty_Internal_Runtime_Inheritance->callBlock(object(Block_19810866863e3665eb94015_33088710), object(SmartyCustomTemplate))
in smarty_internal_runtime_inheritance.php line 184
at Smarty_Internal_Runtime_Inheritance->process(object(SmartyCustomTemplate), object(Block_19810866863e3665eb94015_33088710))
in smarty_internal_runtime_inheritance.php line 156
at Smarty_Internal_Runtime_Inheritance->instanceBlock(object(SmartyCustomTemplate), 'Block_19810866863e3665eb94015_33088710', 'footer')
in e2edce3fe10e6aa05aff7c1599af26310b1ccbc7_2.file.layout-both-columns.tpl.php line 116
at content_63e3665eb98d98_63460293(object(SmartyCustomTemplate))
in smarty_template_resource_base.php line 123
at Smarty_Template_Resource_Base->getRenderedTemplateCode(object(SmartyCustomTemplate))
in smarty_template_compiled.php line 114
at Smarty_Template_Compiled->render(object(SmartyCustomTemplate))
in smarty_internal_template.php line 216
at Smarty_Internal_Template->render()
in smarty_internal_template.php line 385
at Smarty_Internal_Template->_subTemplateRender('layouts/layout-both-columns.tpl', null, 'at_moviclayouts/layout-left-column.tpl', 0, 31536000, array(), 2, false, null, null)
in smarty_internal_runtime_inheritance.php line 116
at Smarty_Internal_Runtime_Inheritance->endChild(object(SmartyCustomTemplate), 'layouts/layout-both-columns.tpl')
in a1098b2decf1c96dbbc1848914cf81e44e633a60_2.file.layout-left-column.tpl.php line 38
at content_63e3665eb7f348_92763673(object(SmartyCustomTemplate))
in smarty_template_resource_base.php line 123
at Smarty_Template_Resource_Base->getRenderedTemplateCode(object(SmartyCustomTemplate))
in smarty_template_compiled.php line 114
at Smarty_Template_Compiled->render(object(SmartyCustomTemplate))
in smarty_internal_template.php line 216
at Smarty_Internal_Template->render()
in smarty_internal_template.php line 385
at Smarty_Internal_Template->_subTemplateRender('layouts/layout-left-column.tpl', null, 'at_moviclayouts/layout-left-column.tpl', 0, 31536000, array(), 2, false, null, null)
in smarty_internal_runtime_inheritance.php line 116
at Smarty_Internal_Runtime_Inheritance->endChild(object(SmartyCustomTemplate), 'layouts/layout-left-column.tpl')
in e29f4ab1fe55858e6b724549e36e5445b5a2720b_2.file.page.tpl.php line 33
at content_63e3665eb76d59_43901630(object(SmartyCustomTemplate))
in smarty_template_resource_base.php line 123
at Smarty_Template_Resource_Base->getRenderedTemplateCode(object(SmartyCustomTemplate))
in smarty_template_compiled.php line 114
at Smarty_Template_Compiled->render(object(SmartyCustomTemplate))
in smarty_internal_template.php line 216
at Smarty_Internal_Template->render()
in smarty_internal_template.php line 385
at Smarty_Internal_Template->_subTemplateRender('page.tpl', null, 'at_moviclayouts/layout-left-column.tpl', 0, 31536000, array(), 2, false, null, null)
in smarty_internal_runtime_inheritance.php line 116
at Smarty_Internal_Runtime_Inheritance->endChild(object(SmartyCustomTemplate), 'page.tpl')
in df1c39e008529093e0cd15f27cf1f604eb7202df_2.file.contact.tpl.php line 50
at content_63e3665eb6b637_82232812(object(SmartyCustomTemplate))
in smarty_template_resource_base.php line 123
at Smarty_Template_Resource_Base->getRenderedTemplateCode(object(SmartyCustomTemplate))
in smarty_template_compiled.php line 114
at Smarty_Template_Compiled->render(object(SmartyCustomTemplate))
in smarty_internal_template.php line 216
at Smarty_Internal_Template->render(false, 0)
in smarty_internal_templatebase.php line 232
at Smarty_Internal_TemplateBase->_execute(object(SmartyCustomTemplate), null, 'at_moviclayouts/layout-left-column.tpl', null, 0)
in smarty_internal_templatebase.php line 116
at Smarty_Internal_TemplateBase->fetch('contact.tpl', null, 'at_moviclayouts/layout-left-column.tpl', null, false, true, false)
in SmartyCustom.php line 112
at SmartyCustomCore->fetch('contact.tpl', null, 'at_moviclayouts/layout-left-column.tpl')
in FrontController.php line 727
at FrontControllerCore->smartyOutputContent('contact.tpl')
in FrontController.php line 709
at FrontControllerCore->display()
in Controller.php line 326
at ControllerCore->run()
in Dispatcher.php line 525
at DispatcherCore->dispatch()
in index.php line 28
I updated prestashop and modules via "1-Click Upgrade" and rolled back to default templates

snmpbulkwalk returning a string in PHP table. How to separate?

Sorry been working this data for hours and can come up with a way to make this work.
I'm performing a php shell_exec snmpbulkwalk.
Example:
$counts = shell_exec("/bin/snmpbulkwalk -v 2c $hostname ..... | awk '{print \$4}' ");
I'm trying to print it in a table.
Example:
print "<table border=1 bgcolor=#ffffff>";
print "<tr>
<td>Counts</td>
</tr>";
for ($i=0; $i<count($counts); $i++) {
print "<tr>";
print "<td>$counts[$i]</td>";
print "</tr>"; }
print "</table>";
The is resulting in the snmpbulkwalk data being put in one cell.
Example:
Counts
890 718 600 577 602 309 924 685 651 204 485 709 341 369 3 39 728 638 556 3 1013 639 476 591 672 774 307 736 586 507 539 467 684 609 760 839 721 735 431 795 868 491 587 581 575 414 299 234
I was looking for each value to be in its own row.
Example:
Counts
890
718
600
577
...
This seems basic but I'm not sure I understand how to fix.
I know in the past if displayed with the php echo below. However I need to pull this data into a table.
Example:
echo "<pre>".$counts."</pre>"
Thanks for any help.

My While Statement is not clearing mysqli_query on second run. Is this normal?

I am trying to create an un-ordered list of weekly dates. But these dates should only populated what is applicable. In my db i have entries with dates ranging from 10/17 - 11/5.
the idea is to get the first date available then search for it and create the 1st week
<li>date - date</li>
Then add a week and search for a date in that week and print the following week sequence, then add a week, etc...
Below is my code:
$fromDt_info = mysqli_query($con,"SELECT * FROM " . $rec_table . " WHERE `date`='".$from_dt."'" );
echo '<ul>';
while($row = mysqli_fetch_array($fromDt_info)){
echo $row['0'];
$to_dt = date('Y-m-d',strtotime($from_dt . '+ 6 Days'));
echo '<li>'.date('m/d/Y',strtotime($from_dt)).' - '.date('m/d/Y',strtotime($to_dt)).'</li>';
$from_dt = date('Y-m-d',strtotime($from_dt . '+ 7 Days'));
//empty array somehow? Prints as many emtries for first date only. But prints updated dates in sequance.
}
echo '</ul>';
This is the result i am getting:
44 10/17/2014 - 10/23/2014
45 10/24/2014 - 10/30/2014
46 10/31/2014 - 11/06/2014
47 11/07/2014 - 11/13/2014
48 11/14/2014 - 11/20/2014
you can see it's printing two extra lines that i don't have entries for.
When i added the echo $row['0']; to print the id, then i noted it's printing all of the entries for the first date, which is five entries on that first date, but the dates printed are squential not relating to the id entry.
Do i need to somehow empty the array at the end of the while statement?
This is the Data in my db:
id date
44 2014-10-17
45 2014-10-17
46 2014-10-17
47 2014-10-17
48 2014-10-17
51 2014-10-20
52 2014-10-20
53 2014-10-20
55 2014-10-20
56 2014-10-20
57 2014-10-20
58 2014-10-21
59 2014-10-21
60 2014-10-21
61 2014-10-21
62 2014-10-21
63 2014-10-21
64 2014-10-21
65 2014-10-22
66 2014-10-22
67 2014-10-22
68 2014-10-22
69 2014-10-23
70 2014-10-23
71 2014-10-23
72 2014-10-24
73 2014-10-24
278 2014-10-27
279 2014-10-27
280 2014-10-27
281 2014-10-27
282 2014-10-27
283 2014-10-27
284 2014-10-27
285 2014-10-28
286 2014-10-28
287 2014-10-28
288 2014-10-29
289 2014-10-29
290 2014-10-29
291 2014-10-29
293 2014-10-30
294 2014-10-30
295 2014-10-30
296 2014-10-30
297 2014-10-30
298 2014-10-30
299 2014-10-31
300 2014-10-31
301 2014-10-31
302 2014-10-31
303 2014-10-31
304 2014-10-31
305 2014-11-03
306 2014-11-03
307 2014-11-03
308 2014-11-03
309 2014-11-04
310 2014-11-04
311 2014-11-04
312 2014-11-04
313 2014-11-04
314 2014-11-05
315 2014-11-05
316 2014-11-05
317 2014-11-05
318 2014-11-05
319 2014-11-05
320 2014-11-05

Finding and removing outliers in PHP

Suppose I sample a selection of database records that return the following numbers:
20.50, 80.30, 70.95, 15.25, 99.97, 85.56, 69.77
Is there an algorithm that can be efficiently implemented in PHP to find the outliers (if there are any) from an array of floats based on how far they deviate from the mean?
Ok let's assume you have your data points in an array like so:
<?php $dataset = array(20.50, 80.30, 70.95, 15.25, 99.97, 85.56, 69.77); ?>
Then you can use the following function (see comments for what is happening) to remove all numbers that fall outside of the mean +/- the standard deviation times a magnitude you set (defaults to 1):
<?php
function remove_outliers($dataset, $magnitude = 1) {
$count = count($dataset);
$mean = array_sum($dataset) / $count; // Calculate the mean
$deviation = sqrt(array_sum(array_map("sd_square", $dataset, array_fill(0, $count, $mean))) / $count) * $magnitude; // Calculate standard deviation and times by magnitude
return array_filter($dataset, function($x) use ($mean, $deviation) { return ($x <= $mean + $deviation && $x >= $mean - $deviation); }); // Return filtered array of values that lie within $mean +- $deviation.
}
function sd_square($x, $mean) {
return pow($x - $mean, 2);
}
?>
For your example this function returns the following with a magnitude of 1:
Array
(
[1] => 80.3
[2] => 70.95
[5] => 85.56
[6] => 69.77
)
For a normally distributed set of data, removes values more than 3 standard deviations from the mean.
<?php
function remove_outliers($array) {
if(count($array) == 0) {
return $array;
}
$ret = array();
$mean = array_sum($array)/count($array);
$stddev = stats_standard_deviation($array);
$outlier = 3 * $stddev;
foreach($array as $a) {
if(!abs($a - $mean) > $outlier) {
$ret[] = $a;
}
}
return $ret;
}
Topic: Detecting local, additive outliers in unordered arrays by walking a small window through the array and calculating the standard deviation for a certain range of values.
Good morning folks,
here is my solution much to late, but since I was looking for detecting outliers via PHP and could'nt find anything basic, I decided somehow smoothing a given dataset in a timeline of 24 h by simply moving a range of 5 items in a row through an unordered array and calculate the local standard deviation to detect the additive outliers.
The first function will simply calculate the average and deviation of a given array, where $col means the column with the values (sorry for the freegrades, this means that in an uncomplete dataset of 5 values you only have 4 freegrades - I don't know the exact english word for Freiheitsgrade):
function analytics_stat ($arr,$col,$freegrades = 0) {
// calculate average called mu
$mu = 0;
foreach ($arr as $row) {
$mu += $row[$col];
}
$mu = $mu / count($arr);
// calculate empiric standard deviation called sigma
$sigma = 0;
foreach ($arr as $row) {
$sigma += pow(($mu - $row[$col]),2);
}
$sigma = sqrt($sigma / (count($arr) - $freegrades));
return [$mu,$sigma];
}
Now its time for the core function, which will move through the given array and create a new array with the result. Margin means the factor to multiply the deviation with, since only one Sigma detects to many outliers, whereas more than 1.7 seems to high:
function analytics_detect_local_outliers ($arr,$col,$range,$margin = 1.0) {
$count = count($arr);
if ($count < $range) return false;
// the initial state of each value is NOT OUTLIER
$arr_result = [];
for ($i = 0;$i < $count;$i++) {
$arr_result[$i] = false;
}
$max = $count - $range + 1;
for ($i = 0;$i < $max;$i++) {
// calculate mu and sigma for current interval
// remember that 5 values will determine the divisor 4 for sigma
// since we only look at a part of the hole data set
$stat = analytics_stat(array_slice($arr,$i,$range),$col,1);
// a value in this interval counts, if it's found outside our defined sigma interval
$range_max = $i + $range;
for ($j = $i;$j < $range_max;$j++) {
if (abs($arr[$j][$col] - $stat[0]) > $margin * $stat[1]) {
$arr_result[$j] = true;
// this would be the place to add a counter to isolate
// real outliers from sudden steps in our data set
}
}
}
return $arr_result;
}
And finally comes the test function with random values in an array with length 24.
As for margin I was curious and choose the Golden Cut PHI = 1.618 ... since I really like this number and some Excel test results have led me to a margin of 1.7, above which outliers very rarelly were detected. The range of 5 is variable, but for me this was enough. So for every 5 values in a row there will be a calculation:
function test_outliers () {
// create 2 dimensional data array with items [hour,value]
$arr = [];
for ($i = 0;$i < 24;$i++) {
$arr[$i] = [$i,rand(0,500)];
}
// set parameter for detection algorithm
$result = [];
$col = 1;
$range = 5;
$margin = 1.618;
$result = analytics_detect_local_outliers ($arr,$col,$range,$margin);
// display results
echo "<p style='font-size:8pt;'>";
for ($i = 0;$i < 24;$i++) {
if ($result[$i]) echo "♦".$arr[$i][1]."♦ "; else echo $arr[$i][1]." ";
}
echo "</p>";
}
After 20 calls of the test function I got these results:
417 140 372 131 449 26 192 222 320 349 94 147 201 ♦342♦ 123 16 15
♦490♦ 78 190 ♦434♦ 27 3 276
379 440 198 135 22 461 208 376 286 ♦73♦ 331 358 341 14 112 190 110 266
350 232 265 ♦63♦ 90 94
228 ♦392♦ 130 134 170 ♦485♦ 17 463 13 326 47 439 430 151 268 172 342
445 477 ♦21♦ 421 440 219 95
88 121 292 255 ♦16♦ 223 244 109 127 231 370 16 93 379 218 87 ♦335♦ 150
84 181 25 280 15 406
85 252 310 122 188 302 ♦13♦ 439 254 414 423 216 456 321 85 61 215 7
297 337 204 210 106 149
345 411 308 360 308 346 ♦451♦ ♦77♦ 16 498 331 160 142 102 ♦496♦ 220
107 143 ♦241♦ 113 82 355 114 452
490 222 412 94 2 ♦480♦ 181 149 41 110 220 ♦477♦ 278 349 73 186 135 181
♦39♦ 136 284 340 165 438
147 311 246 449 396 328 330 280 453 374 214 289 489 185 445 86 426 246
319 ♦30♦ 436 290 384 232
442 302 ♦436♦ 50 114 15 21 93 ♦376♦ 416 439 ♦222♦ 398 237 234 44 102
464 204 421 161 330 396 461
498 320 105 22 281 168 381 216 435 360 19 ♦402♦ 131 128 66 187 291 459
319 433 86 84 325 247
440 491 381 491 ♦22♦ 412 33 273 256 331 79 452 314 485 66 138 116 356
290 190 336 178 298 218
394 439 387 ♦80♦ 463 369 ♦104♦ 388 465 455 ♦246♦ 499 70 431 360 ♦22♦
203 280 241 319 ♦34♦ 238 439 497
485 289 249 ♦416♦ 228 166 217 186 184 ♦356♦ 142 166 26 91 70 ♦466♦ 177
357 298 443 307 387 373 209
338 166 90 122 442 429 499 293 ♦41♦ 159 395 79 307 91 325 91 162 211
85 189 278 251 224 481
77 196 37 326 230 281 ♦73♦ 334 159 490 127 365 37 57 246 26 285 468
228 181 74 ♦455♦ 119 435
328 3 216 149 217 348 65 433 164 473 465 145 341 112 462 396 168 251
351 43 320 123 181 198
216 213 249 219 ♦29♦ 255 100 216 181 233 33 47 344 383 ♦94♦ 323 440
187 79 403 139 382 37 395
366 450 263 160 290 ♦126♦ 304 307 335 396 458 195 171 493 270 434 222
401 38 383 158 355 311 150
402 339 382 97 125 88 300 332 250 ♦86♦ 362 214 448 67 114 ♦354♦ 140 16
♦354♦ 109 0 168 127 89
450 5 232 155 159 264 214 ♦416♦ 51 429 372 230 298 232 251 207 ♦322♦
160 148 206 293 446 111 338
I hope, this will help anyone in the present or future.
Greetings
P.S. To further improve this algorithm you may add a counter, which makes sure, that a certain value must for instance be found at least 2 times, that means in 2 different intervals or windows, before it is labeled as outlier. So a sudden jump of the following values does not make the first value the villain. Let me give you an example:
In 3,6,5,9,37,40,42,51,98,39,33,45 there is an obvious step from 9 to 37 and an isolated value 98. I would like to detect 98, but not 9 or 37.
The first interval 3,6,5,9,37 would detect 37, the second interval 6,5,9,37,40 not. So we would not detect 37, since there is only one problematic interval or one match. Now it should be clear, that 98 counts in 5 intervals and is therefore an outlier. So lets declare a value an outlier, if it "counts" at least 2 times.
Like so often we have to look closely the borders, since they have only one interval, and make for these values an exception.

PHP: echoing a few selected lines of a txt file

I need to echo only selected lines from a .txt file using PHP.
The txt file content looks like this:
1 111 111 111 111 111 111 111 111 111 111
2 196 182 227 190 195 196 278 197 323 265
3 84.1 84.1 84.1 84.2 85.4 84.1 84.2 84.1 84.1 84.1
4 107 107 107 107 107 107 107 107 107 107
5 10.0 9.29 9.86 9.90 9.57 9.56 9.52 9.55 10.0 9.62
6 0.652 0.622 0.676 0.617 0.637 0.617 0.563 0.601 0.586 0.601
7 132 132 132 132 132 132 481 132 132 132
8 113 113 113 113 113 113 113 113 113 113
9 510 571 604 670 647 667 656 257 264 431
10 245 246 247 246 246 245 247 246 247 247
The previous working code was echoing every lines.
$fp = fopen("http://xxx/myfile.txt","r");
while (!feof($fp)) {
$page .= fgets($fp, 4096);
}
fclose($fp);
echo $page;
break;
I'm trying to get the same formating but only with the selected lines.
For instance let say only lines: 3, 5, 8, 10.
The second step would then be if the lines could be echoed in a different order than the initial one.
If someone knows a simple way to do it, that would be excellent!
Thanx in advance.
$lines = file('http://xxx/myfile.txt');
print $lines[2]; // line 3
print $lines[4]; // line 5
...
see file()
here's an example of how it can be done:
function echo_selected_lines_of_file($file, array $lines)
{
$fContents = file_get_contents($file); // read the file
$fLines = explode("\n", $fContents); // split the lines
foreach($fLines as $key=>$fLine) // foreach line...
if(in_array($key, $lines)) //if its in the $lines array, print it
echo $fLine.'<br>';
}
echo_selected_lines_of_file('index.php', array(1,4,7));
You could use the file() which converts each line into a array. But if your file is big it will consume a bit of memory.
$lines_to_print = [1,3,5,8];
$lines = file('file.txt');
for($i in $line_to_print) {
echo $lines[$i];
}
An efficient solution is
$i = 0;
$lines_to_print = [1,3,5,8];
$file = fopen("file.txt", "r");
while(!feof($file)){
$line = fgets($file);
if(in_array(++$i, $lines_to_print)) {
echo $line
}
}
fclose($file);

Categories