How to preserve html character encodings in URL's - php

So I have these long URL's that enter a PHP script through a GET veriable.
<?php
$given_url = $_GET['url'];
echo $given_url;
?>
Lets say I do
http://example.com/index.php?url=http://www.falstad.com/circuit/circuitjs.html?cct=%24+1+0.000005+10.200277308269968+33+5+43%0Ai+112+320+112+192+0+4%0Ai+416+192+416+320+0+6%0Ar+192+320+192+192+0+1%0Ar+208+192+320+192+0+2%0Ar+336+192+336+320+0+3%0Aw+112+192+192+192+0%0Aw+192+192+208+192+0%0Aw+320+192+336+192+0%0Aw+336+192+416+192+0%0Aw+416+320+336+320+0%0Aw+336+320+192+320+0%0Aw+192+320+112+320+0%0A
then how do I let the page echo literaly what is behind the http://example.com/index.php?url= ?
corrently is returns www.falstad.com/circuit/circuitjs.html?cct=$ 1 0.000005 10.200277308269968 33 5 43 i 112 320 112 192 0 4 i 416 192 416 320 0 6 r 192 320 192 192 0 1 r 208 192 320 192 0 2 r 336 192 336 320 0 3 w 112 192 192 192 0 w 192 192 208 192 0 w 320 192 336 192 0 w 336 192 416 192 0 w 416 320 336 320 0 w 336 320 192 320 0 w 192 320 112 320 0 as if it should process the encoded(?) characters in the URL..

Use urlencode()
echo urlencode($given_url);

Try
<?php
$given_url = $_GET["url"];
$pieces = explode("?", $given_url);
$part1 = $pieces[0];
$part2 = urlencode($pieces[1]);
$new_url = $part1.$part2;
echo $new_url
?>
Caveat here is that we are assuming there isn't another "?" somewhere else in the string.

Related

ASCII converter is showing blank with wrong input - PHP

I'm trying to convert ASCII to characters with the following script.
<?php
$decencrypt = explode(" ", $_POST['decryptinput']);
for($i=0;$i<=count($decencrypt)-1;$i++){
echo(chr($decencrypt[$i]));
}
?>
It works with text like: Lorem Ipsum (Encrypted in ASCII)
But when I use input: for($i=0;$i<=count($decencrypt)-1;$i++){for($i=0;$i<=count($decencrypt)-1;$i++){
(f.e.)
It won't work and my site just shows blank.
Can someone help me out so every possible input will work?
Notice that the input looks like this: 116 101 115 116 32 116 101 120 116 32 102 111 114 32 115 116 97 99 107 111 118 101 114 102 108 111 119 13 10
I think this is what you are looking for.
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
$decryptinput = '116 101 115 116 32 116 101 120 116 32 102 111 114 32 115 116 97 99 107 111 118 101 114 102 108 111 119 13 10';
echo $decryptinput.'<br>';
echo asciiDecode($decryptinput);
echo '<br>';
echo asciiEncode("test text for stackoverflow\r\n");
function asciiDecode($iAsciiStr){
$retStr = '';
foreach(explode(' ', $iAsciiStr) as $key=>$val){
$retStr .= chr($val);
}
return $retStr;
}
function asciiEncode($iDecStr){
$retStr = '';
foreach(str_split($iDecStr) as $key=>$val){
$retStr .= ord($val).' ';
}
return $retStr;
}

Find and spilit numbers from a string PHP

I have textarea, string :
__A 59.202x5p.
__B 611.08 500p
__C 991,70p.66.113.552.77.88.10p 199x200p
__C2 33 44x100p 55 161x150p 25 33 85x60p 727 77 373 22x220p
__C3 44 16 59x10p 343 x15p 172 200p
i want output like this :
__A 59.20 02x5p.
__B 61 11.08 500p
__C 99 91,70p.66.11 13.55 52.77.88.10p 19 99x200p
__C2 33 22 44x100p 55 16 61 x150p 25 33 85x60p 72 27 77 37 73 22x220p
__C3 44 16 59x10p 34 43 x15p 17 72 200p
If number is hundreds and before "x ? p" or " ?p" ( ? is random number and cant spilit ), it will spilit and line will like this :
__A 59.202x5p. >>> __A 59.20 02x5p.
__B 611.08 500p >>> __B 61 11.08 500p
__C 991,70p.66.113.552.77.88.10p 199x200p >>> __C 99 91,70p.66.11 13.55 52.77.88.10p 19 99x200p
...
I use preg_match + preg_replace + substr but i cant locate where is hundreds number before "x ? p" or " ?p" ( ? is random number and cant spilit )...
And i dont understand how to spilit number like :
__A 59."202"x5p. ( 202 to 20 02 ) >>> __A 59.20 02x5p.
__B 611.08 500p ( 611 to 61 11 ) >>> __B 61 11.08 500p
My English language not good, hope who read my question can understand and help me solve it.
Thank very very much.
Check the following code..
<?php
echo "<u>CURRENT STRING</u><br/>";
echo $value ="__A 59.202x5p.
__B 611.08 500p
__C 991,70p.66.113.552.77.88.10p 199x200p
__C2 33 44x100p 55 161x150p 25 33 85x60p 727 77 373 22x220p
__C3 44 16 59x10p 343 x15p 172 200p";echo '<br>';
for($i=0;$i<=(strlen($value)-4); $i++ ) {
$myvar = $value[$i].$value[$i+1].$value[$i+2];
if (preg_match("/\d{3}/u", $myvar) > 0 && $myvar>100 && strpos($myvar.$value[$i+3], 'p') == 0)
$value = substr($value,0,$i+2).' '.$value[$i+1].substr($value,$i+2,strlen($value));
}
echo "<u>DESIRED STRING</u><br/>";
echo $value;
?>

Twitter api 1.1 is not loading all followers

I have a program to get followers details of a person. the code is working fine upto the follower count 3000. i tried with another person which have 200000 Followers. Unfortunately it is showing only 300 followers why this happen? is there any way to fix this?
Here is my code
<?php
ini_set('max_execution_time', '50000000');
ini_set('post_max_size', '100M');
require_once('TwitterAPIExchange.php');
$consumerKey = 'xxxxxxxxxxxxxxxxxx';
$consumerKeySecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$accessToken = 'xxxxxxxxxxxxxxxxxxxxxxxxx';
$accessTokenSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$settings = array(
'oauth_access_token' => $accessToken,
'oauth_access_token_secret' => $accessTokenSecret,
'consumer_key' => $consumerKey,
'consumer_secret' => $consumerKeySecret
);
$i = 0;
$cursor = -1;
do {
$url = 'https://api.twitter.com/1.1/followers/list.json';
$getfield = '?cursor='.$cursor.'&screen_name=BeingSalmanKhan&skip_status=true&include_user_entities=false';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$response = json_decode($response, true);
$errors = $response["errors"];
if (!empty($errors)) {
foreach($errors as $error){
$code = $error['code'];
$msg = $error['message'];
echo "<br><br>Error " . $code . ": " . $msg;
}
$cursor = 0;
}
else {
$users = $response['users'];
print_r($users);
echo'<table>';
echo '<tr>';
echo '<td>No:</td>';
echo '<td>Name</td>';
echo '<td>Profile Description</td>';
echo '<td>Location</td>';
echo '<td>Followers Count</td>';
echo '<td>Website Url</td>';
echo '<td>Screen Name</td>';
echo '<td>Favourited Tweets</td>';
echo '<td>Language</td>';
echo '<td>Friends Count</td>';
echo '<td>Status</td>';
echo '<td>Image</td>';
echo '</tr>';
foreach($users as $user){
$thumb = $user['profile_image_url'];
$url = $user['screen_name'];
$name = $user['name'];
$description = $user['description'];
$location = $user['location'];
$followers_count = $user['followers_count'];
$url = $user['url'];
$screen_name = $user['screen_name'];
$favourites_count = $user['favourites_count'];
$language = $user['lang'];
$listed_count = $user['listed_count'];
$friends_count = $user['friends_count'];
$status = $user['status'];
echo '<tr>';
echo '<td>'.$i.'</td>';
echo '<td>'.$name.'</td>';
echo '<td>'.$description.'</td>';
echo '<td>'.$location.'</td>';
echo '<td>'.$followers_count.'</td>';
echo '<td>'.$url.'</td>';
echo '<td>'.$screen_name.'</td>';
echo '<td>'.$favourites_count.'</td>';
echo '<td>'.$language.'</td>';
echo '<td>'.$friends_count.'</td>';
echo '<td>'.$status.'</td>';
echo '<td><img src="'.$thumb.'"></td>';
echo '</tr>';
$i++;
}
$cursor = $response["next_cursor"];
}
}
while ( $cursor != 0 );
if (!empty($users)) {
echo '<br><br>Total: ' . $i;
}
?>
Here is the output what i get
0 Rao.navneet101#gmail 0
1 shivangi darshan dos 0
2 Renuka.R.K 0
3 md samiullah khan bokaro steel city 0
4 monusahu 0
5 Vivek mishra 0
6 Mezba Alam Dhaka, Bangladesh. 20
7 shiva krishnam raju 0
8 shankar rupani 0
9 Vasu patil 0
10 keerthi 0
11 love_guru 1
12 abhishek tiwari 0
13 Future Care 0
14 harikumar sreekumar 3
15 Shahnawaz Khan 0
16 prakhar bhushan 0
17 Binita Chhaparia 0
18 venkatesan 1
19 Rahul RJ 0
20 emty Abuja Nigeria 12
21 Nil-Akash Chy smart 0
22 ashok kumar 1
23 azhar 0
24 Prarthana 0
25 Anu bibu 0
26 SAMIR SINGH 0
27 Deemag ki Maa Behen. Saket, New Delhi 35
28 abel leo 0
29 Dhananjay Pawar 0
30 Anuradha Choudhary 1
31 maiome.maiome 0
32 rahul hussey 1
33 vishnupriya 3
34 anggi12345 5
35 farheen naaz haora 0
36 aman 4
37 Shubham Verma Varanasi(UP) 0
38 satish kumar jaiswar 0
39 sheikhwasi 0
40 MUHAMMADUMAR 0
41 Gaurav tiwari India 0
42 arjun malviy 0
43 prashanth 0
44 saloni 0
45 Tanvir,Hridoy 0
46 Mahesh Sharma 0
47 Deepak Aswathnarayan 2
48 devender kumar 0
49 Awal 0
50 Sanketa Kamble 0
51 faraz azhar 1
52 Avinash singh 0
53 KUMAR SUMIT 0
54 Mahuya sultana 0
55 hemant chawla 0
56 Hanii andiraa 0
57 mahendra shah AHMEDABAD 5
58 Angel Preet 0
59 kumar gaurav 0
60 atul kumar bangalore 1
61 saurabh singh 0
62 ajaygadhavi 0
63 Prajkta Waditwar Mumbai 21
64 Shruti 1
65 Prabhakar Gupta 1
66 waseem abbas 0
67 Malik Zulqarnain 0
68 Sk Azharuddin 0
69 MOHIT VIJAY 0
70 RadhaKrishnan chennai 22
71 Ruchita Chaudhari 0
72 MANISH RAWAT 0
73 vyasronit vansba 0
74 SURAJ YADAV 1
75 Akanksha Pratik 0
76 Sandeep goswami 0
77 Rupinder kaur 0
78 abhishek pandey 3
79 imad 2
80 Sandeep rao 0
81 sahil khan 0
82 abdulbari 95
83 Binal Chitroda. 0
84 Sexy boy 0
85 Akash chauhan 1
86 qawserftgyhujik 8
87 dhruvil patel 0
88 Barada sahoo 0
89 Banu 2
90 Uddipta kashyap 0
91 Mitul sharma Jammu 0
92 pankaj singh faridabad, haryana 0
93 Sanjeev krishnan 0
94 adnan ahmed 0
95 Ahad sheikh 0
96 manish shah Rajkot 5
97 VISHAL SINGH 1
98 aksahy 1
99 satya prasanna kkd 23
100 rajesh rana 0
101 Jatt Boys 1
102 Zeel Doshi 22
103 nabin regmi 1
104 aneeta awasthi 0
105 navjit k chopra 1
106 Ashim Mallick 0
107 Rajesh Kumar Mishra 3
108 Rahul pagare 0
109 Lingam k 0
110 Abishek bagmar chennai 32
111 Trang Weinman 0
112 muktadesai 0
113 mansur.ali2009#gmail 0
114 Angel Urvashi amritsar 1
115 rangga nurmansyah 2
116 Rajesh Shetty 1
117 Muhammad Sohail Akba 1
118 waroeng sehat HI 0
119 Montibohra 0
120 siddarth 1
121 SHRAVANI KURRA 0
122 suven sarkar 0
123 ajit suryawanshi 0
124 pappu rakade patil Babra Aurangabad Maharastra 0
125 shiyad shereef 8
126 Sachin Ingale Pune 0
127 archana mishra 0
128 vijayjaware 1
129 Alive Soul 0
130 aakash malhotra 0
131 sheikh mohsin 0
132 Sheryll Franca 0
133 Manjeet Mundhe 6
134 khan sania hong kong 0
135 vishaldev 0
136 grewal laddi 1
137 Sanjay kumar 0
138 j aishwarya rao 2
139 didar khan oman.sadah 0
140 SONI SINGH 0
141 mohit khandelwal 2
142 sunny verma 1
143 Mohinurgazi 0
144 Jitender Kumar 1
145 Vinay jayakumar 0
146 solomonrajesh chennai 2
147 k.nagalakshmi 1
148 jeevankiran 9
149 Raghu 0
150 Alive Chatulistiwa 10
151 ses dubey 0
152 Sumit Zadafiya 1
153 majid abass hassanabad rainawari srinagar 3
154 shubh jain 0
155 M Fuad 0
156 poojashadhijha 0
157 manu garg 0
158 Imran Hussain 0
159 Zain jutt 0
160 k.seven new delhi 0
161 Lakhan Wanole 1
162 Olympia Verrell 0
163 majuanwar 0
164 Page 29 Kolkata, India 0
165 aayushmaan 19
166 prashant gupta 3
167 jose santha seelan 19
168 Mahar Ejaz 1
169 kabeer magsi 0
170 PriNcE SaMeeR 0
171 prashant shrivastava 0
172 Vinod Ghorpade 0
173 eno apriliani 1
174 Pratik Wadkar 0
175 FiRu Kuwait 3
176 Jagtap 0
177 Ravi kumar Giridih 0
178 Ajay Khandelwal 1
179 poojamaurya 0
180 Rajat Chouhan 0
181 Ayanna Nelms 0
182 shrimanth kumar 2
183 SATYANARAYNANDWANA 0
184 AJEESH Ayoor,kollam 10
185 Shah Alam 1
186 Flywell2India ON L5A 1W7, Canada 3
187 banti kashyap jaipur 1
188 ayesha pathan India 10
189 Ranjan SP 44
190 AmarDeep 0
191 Manish Chamling Rai 0
192 Arun kumar 0
193 Mitesh Baranwal Varanasi 11
194 AKHTAR ABBAS 1
195 Toufiq Alahi 0
196 Rajbir Singh 2
197 Syed Irfan Hussain A 1
198 Syed Anwaar Ali indore, India 0
199 MD.SOWKAT Bangladesh 1
200 srinivasan 1
201 Pradeep Sheler 1
202 sagar shergill 0
203 mohanvamsi 0
204 Keshav Singh 0
205 Ankit Verma 0
206 vinod kachare 5
207 Faraz Imam 3
208 Prateek Pathak 16
209 Kumar Abhishek 1
210 rishita gupta 8
211 Krishna Kumar Tiwari 1
212 KALIM KHAN 4
213 VIPIN 0
214 mukund vishwakarma 0
215 jitendra mishra 1
216 Amit kumar 0
217 Tariq 1
218 Sonu Jani 0
219 Naveen Malethiya Sri Ganganagar 0
220 shyam rajput 0
221 Progressive Dental 0
222 Aryan sid 0
223 simran galhotra 6
224 jot singh 0
225 vishnukumar.merugu 1
226 sujal khandelwal 1
227 shashank patel 0
228 suhail khan 3
229 Vedant Jogdand Pune 8
230 Irfanvali 0
231 sakshi vinayak 2
232 Amrapali R. Sarodey 0
233 ????? 1
234 Purnendu Sharma delhi 1
235 lhomingdolma lama Kathmandu, Nepal 0
236 humaira khanam 1
237 mohammadjawed akhter 1
238 Himanshu Rao 0
239 sandeep nargunde 1
240 yuvarajc 3
241 rajesh 0
242 Vithika Sheru 0
243 himanshu mumbai, maharashtra 6
244 seedtan 0
245 nobaiah143billa 0
246 saurav dhakal Nepal 0
247 Deep Narayan Rai 1
248 sajjad hussain 1
249 deepak chandra 2
250 Naresh Gorre 0
251 priyamaina assam india 1
252 goravsaini 1
253 sherlock 0
254 Nileshsingh 1
255 Aashiqurrahman 0
256 Anushka Singh 1
257 sumit chhari mumbai 1
258 waquarthakur Gulaothi 0
259 rama karki 1
260 pushpa 0
261 hasrat 0
262 pawan sharma 0
263 josey joo 0
264 govinda kr. yadav 0
265 gurnam singh 1
266 MyBusTickets.in India 216
267 vicarsonmilco 0
268 ANKIT GUPTA 0
269 alamin 0
270 Kishore 1
271 aifazz jr sayed 0
272 Salman khan 0
273 Dipesh Jha 0
274 premkumar 1
275 KD Rahees Saifi 2
276 Shafiq mastoi 0
277 Manvi Agarwal 1
278 sumit 1
279 manishkumar h patel 1
Error 88: Rate limit exceeded
Total: 300
280 ajay desai 0
281 Umang Pandita New Delhi 11
282 anu rathour 1
283 Srinivas Kamath 1
284 Pranay Gavhale 1
285 pranjal Guwahati 1
286 Nani_Tasser 0
287 Aryan siddiqui 0
288 Karina Noren 0
289 samyok subba 0
290 sajid malik 0
291 Ritesh kanwar 0
292 kameshnayak2 raipur 1
293 Ashik Babu 1
294 Jenifer Bubak 0
295 divyansh verma kanpur 53
296 Tothi Monsang Manipur, India. 0
297 vikas pathak 1
298 Mohit Rule 1
299 asgar ali 3
At last i can see a "Error 88: Rate limit exceeded" message.
Thanks.
At last i got solution for this. This is the only way i found,
Save the last cursor and sent this cursor after 15 minutes as first cursor. it will show another 300 followers.
Thanks
https://dev.twitter.com/docs/api/1.1/get/followers/ids
Technically, the limit of followers returned each request is 5000.
Are you sure that the count is really 300 in any cases ? Did you display the count of the list you have ?

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.

Was ending character reached or not

In summary I am using stream_get_line to read a line of a file, replace a string and then write the line to another file.
I am using stream_get_line and supplying the "ending" parameter to instruct the function to read lines, or if there is no new line then read 130 bytes.
What I would like to know is how can I know if the 3rd parameter (PHP_EOL) was found, as I need to write exactly the same line (except for my string replacement) to the new file.
For reference...
string stream_get_line ( resource $handle , int $length [, string $ending ] )
It's mainly needed for the last line, sometimes it will contain a newline character and sometimes it doesn't.
My initial idea is to seek to the last line of the file and search the line for a new line character to see if I need to attach a newline to my edited line or not.
You could try using fgets if the stream is in ASCII mode (which only matters on Windows). That function will include the newline if it is found:
$line = fgets(STDIN, 131);
Otherwise, you could use ftell to see how many bytes were read and thus determine whether there was a line ending. For example, if foo.php contains
<?php
while (!feof(STDIN)) {
$pos = ftell(STDIN);
$line = stream_get_line(STDIN, 74, "\n");
$ended = (bool)(ftell(STDIN) - strlen($line) - $pos);
echo ($ended ? "YES " : "NO ") . $line . "\n";
}
executing echo -ne {1..100} '\n2nd to last line\nlast line' | php foo.php will give this output:
NO 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
NO 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 5
NO 3 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
YES 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
YES 2nd to last line
NO last line

Categories