I have this TXT file, which each line I would to insert into array, like this:
Array ( [0] => [1] => 8100 [2] => 623 [3] => 09:04 [4] => AM [5] => 00:26 [6] => L [7] => S-ED [8] => 768 [9] => #4506856439 [10] => 00:01 [11] => )
CO USER TIME DURATION TYPE ACCOUNT_CODE CALLED_NUM RING_TIME
8100 623 09:04 AM 00:26 L S-E 250821613987
8021 8816 09:06 AM 00:20 I S-E D 768 #4506856439 00:01
8020 09:06 AM I N D 603 #45424044499 00:30
8011 09:07 AM 00:11 I S-E D 727 #7546355292 00:02
" 8817 00:11
" 00:02 H
8100 623 09:07 AM 00:01 L S-E 5542204034
8007 8818 09:08 AM 00:13 I S-E D 618 #45269021726 00:01
8013 8811 09:09 AM 00:01 I S-E D 770 #436217227 00:01
8014 09:10 AM 00:16 I S-E D 652 #4523859922 00:01
I'd like to insert it into an array with all parameters, even if empty,
but I can't get it work.
I tried this:
$fh = fopen('captures/131210.txt','r');
while ($line = fgets($fh)) {
$tempexp = preg_split('/\s+/', $line);
print_r($tempexp);
echo "<br />";
}
fclose($fh)
But it gives out weird outputs for some values, like this:
Array ( [0] => [1] => " [2] => 8812 [3] => 00:16 [4] => )
Help would be much appreciated!
If the layout of your text file is purely based on spaces you can used the exact columns to extract the data. Since the columns are not of equal width you cannot simply write it in one loop.
Use the substr function to get part of the line (first number is index, second number is length to read)
$fh = fopen('captures/131210.txt','r');
while ($line = fgets($fh)) {
$co = substr($line, 0, 7);
$user = substr($line, 7, 6);
... etc ...
echo "<br />";
}
fclose($fh)
You might need to parse the separate parts further down dependent on the content and what you want to do with it - parse as integer, date, time etc.
Related
I am working on a project where I have to divide numbers into certain parts.
right now I have 2 numbers max number: 300 and min number 240.
Here a person can add any number such as 5 so all numbers will show in the range such as 300, 285,270,255,240.
My code is:
$amp_starting_price = 300;
$amp_lowest_accepted_price = 240;
$quantity_available = 5);
$r = range($amp_lowest_accepted_price, $amp_starting_price, $quantity_available);
Output: Array (
[0] => 240
[1] => 245
[2] => 250
[3] => 255
[4] => 260
[5] => 265
[6] => 270
[7] => 275
[8] => 280
[9] => 285
[10] => 290
[11] => 295
[12] => 300 )
This is how I need it to show. Divide into 5 or any given number of parts into a particular range such as 300 to 240.
Output: Array (
[0] => 240
[3] => 255
[6] => 270
[9] => 285
[12] => 300 )
I have edited my first answer to fully fit the desired output:
<?php
// set number of parts including the start point
$parts = 5;
// set the increment
$increment = 5;
// set max and min values of the range
$amp_starting_price = 300;
$amp_lowest_accepted_price = 240;
// check if division is exact and all parts will be equal in number
if (($mod = fmod(($amp_starting_price-$amp_lowest_accepted_price), ($parts-1))) == 0) {
$quantity_available = ($amp_starting_price-$amp_lowest_accepted_price)/($parts-1);
} else {
die("Error: Division is not exact. Exist a floating point remainder: $mod\n");
}
// get the increment range
$r_increment = range($amp_lowest_accepted_price, $amp_starting_price, $increment);
// get the parts range
$r_parts = range($amp_lowest_accepted_price, $amp_starting_price, $quantity_available);
// output the result
print_r(array_intersect($r_increment,$r_parts));
It outputs this result:
Array
(
[0] => 240
[3] => 255
[6] => 270
[9] => 285
[12] => 300
)
Suppose an array is give:
$given_array=Array(
[0] => 30
[1] => 45
[2] => 60
[3] => 75
[4] => 90
[5] => 105
[6] => 120
[7] => 135)
A number is given example: 195
Number until 195 needs to be inserted with a difference of 15
So resulting array is:
Array(
[0] => 30
[1] => 45
[2] => 60
[3] => 75
[4] => 90
[5] => 105
[6] => 120
[7] => 135
[8] => 150
[9] => 165
[10] => 180
[11] => 195)
Need to know the best approach to do so time required is least.
So far i have tried:
$given_num=195;
if((given_num-$given_array[count($given_array)-1])!=15 && count($given_array)>0){
while(($given_array[count($given_array)-1]+15)<=given_num){
$given_array[]=$given_array[count($given_array)-1]+15;
}
}
Results are correct but not time feasible
I see that the goal is to have an array with subsequent numbers divisible by 15. Unless you really have to reuse the old array (I wouldn't know why), I
would suggest creating a new array with range():
$given_array = range($given_array[0], 195, 15);
Haven't tried yours and not sure whats wrong with it regarding speed but try this.
<?php
$given_array=[30, 45, 60, 75, 90, 105, 120, 135];
$newNumber = 195;
$count = floor(($newNumber - $given_array[count($given_array)-1]) / 15);
for($i=0; $i<$count; $i++) {
array_push($given_array, $given_array[count($given_array)-1]+15);
}
print_r($given_array);
example: https://3v4l.org/vPiAW
I would say:
$given_num = 195;
$last = end($given_array);
while ($given_num > $last) {
$last = min($last + 15, $given_num);
$given_array[] = $last;
}
But the range-version is quite nice =)
Another short solution using end function:
while (($last = end($given_array)) < 195) $given_array[] = $last+15;
// now, $given_array contains all the needed items
i have a badly formatted text file which i would like to convert to csv.
Here's an example:
100910 NA/1-2013-99636 VIA DEI PESCATORI 2/A LODI APR 8 2013 4:24PM DANNEGGIATO -10% 200 2700 0 0 NO
148013 NA/1-2014-146194 CAVALLOTTI SNC LODI GEN 3 2014 3:37PM DANNEGGIATO -10% 0 0 2 0 NO
160032 NA/1-2014-158129 PAOLO GORINI SNC LODI MAG 6 2014 11:51AM DANNEGGIATO -10% 2 0 2 0 NO
54900 NA/1-2014-158070 STRADA VECCHIA CREMONESE SNC LODI MAG 6 2014 9:53AM DANNEGGIATO +10% 10 0 10 0 NO
100910 NA/1-2013-99636 VIA DEI PESCATORI 2/A LODI APR 8 2013 4:24PM DANNEGGIATO -10% 200 2700 0 0 NO
147959 NA/1-2014-146140 DOSSENA SNC LODI GEN 3 2014 10:45AM DANNEGGIATO -10% 200 0 200 0 NO
That is roughly in this form :
[number] [id] [awfully formatted street] ['LODI'] [timestamp] [damaged or not] [percentage] [squaremeters] [squaremeters] [squaremeters] [squaremeters] [asbest-crumbled or not]
My problem is how to extract the 3rd part, [awfully formatted street].
Basically it's the string after [id] preceding the string ['LODI'] (but ['LODI'] must be just before [timestamp] )
Should i explode() each line by spaces and then traversing the array backwards, overtake [timestamp], overtake ['LODI'] and joining the values before array[id], i.e array [1]? Or is there a smarter (elegant) way to do this, perhaps with preg_match()?
Thanks for any hint!
<?php
// read file line by line
$line = '148013 NA/1-2014-146194 CAVALLOTTI SNC LODI GEN 3 2014 3:37PM DANNEGGIATO -10% 0 0 2 0 NO';
//start by seperating the string on LODI
$lodi_split = explode('LODI', $line);
// Now split the first occ into an array on space
$bits = explode(' ', $lodi_split[0]);
$address = '';
// start reading occurance from occ 2 to loose the first 2 fields
for ($i=2; $i < count($bits); $i++ ) {
$address .= $bits[$i] . ' ';
}
echo $address . PHP_EOL;
Result is
CAVALLOTTI SNC
This should work to extract the address from a row.
<?php
$row = "100910 NA/1-2013-99636 VIA DEI PESCATORI 2/A LODI APR 8 2013 4:24PM DANNEGGIATO -10% 200 2700 0 0 NO";
$row_array = preg_split('/\s+/', $row);
array_shift($row_array);
array_shift($row_array);
for($i=0; $i<12; $i++){
array_pop($row_array);
}
$address = implode(" ", $row_array);
?>
I think explode won't do here. I propose using regexp. For Instance, If you read your .txt file as one string(where data strings are separated with \n):
$f = fopen($fname="file.txt", "rt");
$str = fread($f, filesize($fname)));
fclose($f);
Then use preg_match_all() like this:
$re = "/^(\\d+)\\s*(.*)(LODI)\\s*(.+(?:AM|PM))\\s*(\\w+)\\s+(-?\\d{1,3}%)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\w+)$/m";
preg_match_all($re, $str, $matches,PREG_SET_ORDER );
echo "<pre>\n";
print_r($matches);
echo "</pre>\n";
The output would look like this:
Array
(
[0] => Array
(
[0] => 100910 NA/1-2013-99636 VIA DEI PESCATORI 2/A LODI APR 8 2013 4:24PM DANNEGGIATO -10% 200 2700 0 0 NO
[1] => 100910
[2] => NA/1-2013-99636 VIA DEI PESCATORI 2/A
[3] => LODI
[4] => APR 8 2013 4:24PM
[5] => DANNEGGIATO
[6] => -10%
[7] => 200
[8] => 2700
[9] => 0
[10] => 0
[11] => NO
)
[1] => Array
(
[0] => 148013 NA/1-2014-146194 CAVALLOTTI SNC LODI GEN 3 2014 3:37PM DANNEGGIATO -10% 0 0 2 0 NO
[1] => 148013
[2] => NA/1-2014-146194 CAVALLOTTI SNC
[3] => LODI
[4] => GEN 3 2014 3:37PM
[5] => DANNEGGIATO
[6] => -10%
[7] => 0
[8] => 0
[9] => 2
[10] => 0
[11] => NO
)
..........// And so on
I used the text that you provided above in this example. So in the output you recieve your data formated as list of arrays. So you can do whatever you want with it. $matches[$i][0] - will store the whole match so just skip it and use $matches[$i][1]....$matches[$i][11] as your data.
Can you tell me how I can increment a variable in PHP from 00 to ZZ ?
With A5, 8R, GG...
I tried this but it's just for letter :
for($i="AA"; $i<="ZZ" AND strlen($i)<=2; $i++)
Thank you
i created a little snippet that should demonstrate how you can use range($start,$end) to create what you are looking for
<?php
//create an array with all values from 0-9 and A-Z
$range = array_merge(range("0","9"),range("A","Z"));
//create counter-aray
$counter = array();
//loop through the range
foreach($range as $value1){
foreach($range as $value2){
$counter[] = $value1.$value2;
}
}
//show the counter
print_r($counter);
?>
result:
Array
(
[0] => 00
[1] => 01
[2] => 02
[3] => 03
[4] => 04
[5] => 05
[6] => 06
[7] => 07
[8] => 08
[9] => 09
[10] => 0A
[11] => 0B
[12] => 0C
[13] => 0D
[14] => 0E
[15] => 0F
[16] => 0G
[17] => 0H
[18] => 0I
[19] => 0J
[20] => 0K
[21] => 0L
.....many more values follow here
)
feel free to ask questions if you need further explaination
It actually sounds as if you are trying to add numbers in base 36. Since PHP can convert between bases, you could add the numbers in base 10 then convert into base 36.
for($i = 0; $i <= base_convert("zz", 36, 10); $i++) {
echo(str_pad(strtoupper(base_convert($i, 10, 36)), 2, "0", STR_PAD_LEFT) . PHP_EOL);
}
$i is an integer in base 10 that will loop from 0 to 1295. (zz in base 10.)
base_convert converts $i from base 10 to base 36.
strtoupper converts the resulting string to uppercase so you get AA instead of aa.
str_pad will add leading 0s to convert values such as 0 to 00.
I just got help from some on this site and was able to let a user upload a text file, and have the in the upload process grab the file and programatically search for keywords I specify. The script then counts how many times the word is found and outputs the entire line that it was found in into an array.
So the example results returns this when using this code:
$sceneINT = $sf->countKeyWord('INT', $file);
with my class looking like so:
public static function countKeyWord($word, $file){
if(!$word)
return NULL;
$contents = file_get_contents($file);
#standardise those line endings
$contents = str_replace(array("\r","\r\n","\n\r","\n"),"\n",$contents);
$lines = explode("\n", $contents);
#find your result
$result = $line_num = array();
foreach($lines as $line_num => $l)
if(strpos($l, $word)) {
$result[] = $l;
$line_nums[] = $line_num;
}
echo "<pre>"; // I am echoing out the results for debugging purpuses
print_r($result);
echo "</pre>";
return count($result); //final result shown to the user will only be the count
}
The results from this look like this:
Array
(
[0] => 3 INT. MARTEY'S OFFICE - DAY 3
[1] => 4 INT. RONNEY'S OFFICE - DAY 4
[2] => 6 INT. - BREEZE'S APARTMENT - DAY 6
[3] => 9 INT. - WAREHOUSE/SOUNDSTAGE - DAY 9
[4] => 11 INT. EXAM ROOM - DAY 11
[5] => 12 INT. RAJA'S OFFICE - LATER 12
[6] => 14 INT. RAJA'S OFFICE - LATER 14
[7] => 15 INT. LARGE OPERATING ROOM - DAY 15
[8] => 16 INT. RAJA'S OFFICE - LATER 16
[9] => 17 INT. OLIVER'S CAR - DAY 17
[10] => 20 INT. - ROY THUNDER'S OFFICE - NIGHT 20
[11] => 22 A 2ND CLIP FROM "GOLDEN GATE GUNS"- INT. BASEMENT - DAY 22
[12] => 27 INT. HOUSE WIFE #3'S HOUSE - LATER 27
[13] => 29 INT. LIBRARY - DAY 29
[14] => 31 INT. COFFEE SHOP - NIGHT 31
[15] => 32 INT. WAITING AREA - DAY 32
[16] => 33 INT. CASTING OFFICE - DAY 33
[17] => 34 INT. CASTING OFFICE - DAY 34
[18] => 35 INT. WAITING AREA - DAY 35
[19] => 36 INT. WAITING AREA - LATER 36
[20] => 37 INT. MOTEL ROOM - DAY 37
[21] => INT. WAITING AREA - LATER
[22] => 39 INT. WAITING AREA - DAY 39
[23] => 42 INT. WAITING AREA - DAY 42
[24] => 43 INT. CASTING OFFICE - DAY 43
[25] => 44 INT. AUDITION ROOM - DAY 44
[26] => INT. AUDITION ROOM - DAY
[27] => 45 INT. WAITING AREA - DAY 45
[28] => 46 INT. CASTING OFFICE - DAY 46
[29] => 47 INT. AUDITION ROOM - DAY 47
[30] => 48 INT. CASTING OFFICE - DAY 48
[31] => 49 INT. WAITING AREA - DAY 49
[32] => 50 INT. AUDITION ROOM - DAY 50
[33] => 51 INT. CASTING OFFICE - DAY 51
[34] => 52 INT. WAITING AREA - DAY 52
[35] => 53 INT. CASTING OFFICE - DAY 53
[36] => 54 INT. BURGER JOINT - NIGHT 54
)
I need to upload the results into a database;
Taking Array[0] for example I would need to prepare that line for the database to look like this
scene: 3
int_ext: INT
scene_name: MARTEY'S OFFICE
day_night: DAY
All that will go into 1 row in the database, I don't know how to approach this. How can I take the result and split into what I need and then send it to the database so that ALL items found are stored when the user presses the SAVE button.
Your code has some errors.
You create array $line_num but use $line_nums to add entries
You don't use $line_nums, why do you create it?
I would use a regular expression to find all relevant information:
$contents = file_get_contents($file);
$pattern = "!INT\. (.*?) - (MORNING|NIGHT|DAY|LATER)!si";
preg_match_all($pattern, $contents, $matches);
echo '<pre>';
print_r($matches);
echo '</pre>';
.
If you really need the line number, you have to go another way:
Use preg_replace to replace all line breaks:
//OLD: $contents = str_replace(array("\r","\r\n","\n\r","\n"),"\n",$contents);
//replaces even empty rows
$contents = preg_replace("!(\r|\n|\r\n\r\n|\r\r|\n\n)!s", "\n", $contents);
You can use the the regexp of m4rc to split your data ;-)
Heres a really really quick regex I just did. I'm sure that you can tidy it up a bit but it gives you something to go on. I'd look up http://php.net/manual/en/function.preg-split.php
foreach($result as $line)
{
$splitLine = preg_split("/(\d{1,3})[ ]+([A-Z.]{4}) ([A-Z' ]{1,}) - ([A-Z]{3,})/", $line);
}
That should split it up into an array. I've tested it with the first line of your array. one thing to be aware of is that it wasn't clear whether there were any padding spaces before the first number on each line of the array - so you might have to play around with the regex.