I'm trying to parse each IP line from the following file (loading from the web) and I'm going to store the values in database so i'm looking to put them in to an array.
The file its loading has the following source:
12174 in store for taking<hr>221.223.89.99:8909
<br>123.116.123.71:8909
<br>221.10.162.40:8909
<br>222.135.5.38:8909
<br>120.87.121.122:8909
<br>118.77.254.242:8909
<br>218.6.19.14:8909
<br>113.64.124.85:8909
<br>123.118.243.239:8909
<br>124.205.154.181:8909
<br>124.117.13.116:8909
<br>183.7.223.212:8909
<br>112.239.205.245:8909
<br>118.116.235.156:8909
<br>27.16.28.174:8909
<br>222.221.142.59:8909
<br>114.86.40.251:8909
<br>111.225.105.142:8909
<br>115.56.86.62:8909
<br>59.51.108.142:8909
<br>222.219.39.194:8909
<br>114.244.252.246:8909
<br>202.194.148.41:8909
<br>113.94.174.239:8909
<br><hr>total£º 24¡£
So I guess I'm looking to take everything between the <hr>'s and add each line line by line.
However doing the following doesn't seem to be working (in terms of stripping it the parts i dont' want)
<?php
$fileurl = "**MASKED**";
$lines = file($fileurl);
foreach ($lines as $line_num => $line) {
$line2 = strstr($line, 'taking', 'true');
$line3 = str_replace($line2, '', $line);
print_r($line3);
}
?>
If you want to add the values to an array, why not doing that directly inside the loop? I'd do something like this:
$output = array();
foreach ($lines as $line) {
if(preg_match("/<br>\d/", $line)) {
$output[] = substr($line, 4);
}
}
print_r($output);
Look into PHP function explode: http://www.php.net/manual/en/function.explode.php
It can take a string, and create an array out of it, by splitting at a specific character. In your case, this might be <br>
Also, trim function can get rid of the whitespace when needed.
Related
This is a really odd behavior that I can't explain. I have a CSV file that I'm trying to format. The lines could have trailing ','s that I want to remove.
$lines = explode(PHP_EOL, $csv);
$csv = '';
foreach($lines as $line) {
$csv .= trim($line, ',') . PHP_EOL;
}
The trim is not doing anything and just returning the line back as it is. Just to make sure I copied a line from the csv trim("a,b,c,d,,", ','); which works fine. Can anyone tell me why the above code won't work?
If the CSV file was created on a different operating system, it may use different line breaks than PHP_EOL. So trim any line break characters in addition to commas.
foreach($lines as $line) {
$csv .= trim($line, ",\r\n") . PHP_EOL;
}
Don't manually edit the CSV file. Parse it into an array, then edit the array. Then you can write the modified array back to a CSV file.
You can use fputcsv to write the data to a file, or str_putcsv (a custom function).
$newData = [];
$data = array_map('str_getcsv', $lines); // parse each line as a CSV
foreach ($data as $row) {
$row = array_filter($row); // remove blank values
// for some dumb reason, php has `str_getcsv` but not `str_putcsv`
// so let's use `str_putcsv` from: https://gist.github.com/johanmeiring/2894568
$newData[] = str_putcsv($row);
}
$newData = implode(PHP_EOL, $newData);
I have a big text file, split by new lines and on every line elements split by ';', like this:
1;1;20;3.6;0%;70%;25%;0%;5%;
1;2;80;4;45%;20%;20%;15%;0%;
1;3;80;4;40%;35%;5%;20%;0%;
1;4;20;3.6;15%;40%;38%;5%;2%;
1;5;20;3.6;30%;18%;33%;20%;0%;
1;6;80;4;27%;47%;23%;3%;0%;
What I would like to do is with PHP is to read the file correctly and access a specific element in any row, for example on row 2, element 3 (maybe, [1][2], if considered as indexes) and print it.
<?php
//split by new line
$text = fopen("public/data/data1.txt", "r");
if ($text) {
while (($lines = fgets($text)) !== false) {
//split by ;
$line = explode(';', $lines);
//access a specific element
}
fclose($text);
} else {
// error opening the file.
}
?>
Does somebody know how I could access this elements?
You can explode the string twice.
First on lines, then on ;.
$arr = explode(PHP_EOL, $str);
Foreach($arr as &$line){
$line = explode(";", $line);
}
https://3v4l.org/5fbvZ
Then echo $arr[1][2]; will work as you wanted
Present data in file ---
Kortrijk]]||75,592||74,790||73,777VWVVLG
Hasselt]]||65,503||68,085||70,584VLIVLG
Sint-Niklaas]]||68,277||68,290||70,016VOVVLG
Ostend]]||69,039||67,279||69,115VWVVLG
|22Tournai]]||67,291||67,379||67,844WHTWAL
|23Genk]]||61,532||62,842||64,095VLIVLG
|24Seraing]]||62,832||60,557||61,237WLGWAL
This is the data set i have in my wiki.txt file, i need to remove all content after "]]||" from all lines.
//Require data after code implementation
Kortrijk
Hasselt
Sint-Niklaas
Ostend
|22Tournai
|23Genk
|24Seraing
This is the code i came across, but dont have any idea how to use it in my code, i followed preg_replace, regular expression etc but all going above my head..help me plz and plz let me know any tutorial link that i can follow for these kind of working(specially regular expression for a novice).
$file="wiki.txt";
file_put_contents($file,str_replace('find','replace',file_get_contents($file)));
try:
$arr = file("wiki.txt"); //will give you contents as array
$newContent = "";
foreach($arr as $key => $val) {
$newContent .=substr_replace($val, '', strpos($val, ']'))."\n";
}
//add changed content back to file
file_put_contents("wiki.txt", $newContent);
//result is:
Kortrijk
Hasselt
Sint-Niklaas
Ostend
|22Tournai
|23Genk
|24Seraing
preg_replace('/]]\|\|.*$/m', '', $fnames);
Matches ']]||' literally, then all characters (.*) until the end of a line '$' and replaces them with ''.
Also: Check out this tutorial on RegExp
Rather than "change" the file, you probably want to open the file, read the contents, then write the parts you want to a new file. If everything goes as planned, replace the old file with the new file. Much safer that way.
<?php
$lines = file("input.txt");
$output = "";
foreach ($lines as $line) {
$output .= substr($line, 0, strpos($line, "]")) . "\n";
}
file_put_contents("output.txt", $output);
Lots of ways to solve this.
I'm trying to parse individual values in a tab seperated file with line breaks like so:
00601 166659789 799296 64.348 0.309 -66.749961 18.180555
00602 79288158 4446273 30.613 1.717 -67.17613 18.362268
I'm parsing it right now using:
$delimiter = "\t";
$splitcontents = explode($delimiter, $contentsOfFile);
foreach ( $splitcontents as $value )
{
echo $value;
}
This works, however, when a new line occurs, the last value from the previous line and the first value of the new line are combined. So when the for loop reaches the end of the first line, the last value is actually "18.180555 00602".
How can I parse out values based on line breaks as well as tabs?
Looks like you're just trying to parse a tab-delimited file. Use fgetcsv and assign the delimiter as a tab.
http://us3.php.net/manual/en/function.fgetcsv.php
Explode based on the newline first, then explode each line with the tab.
$delimiter = "\n";
$splitcontents = explode($delimiter, $contentsOfFile);
foreach ( $splitcontents as $line )
{
$bits = explode("\t", $line);
var_dump($bits);
}
I do have a text file having around 400k data in it. and its content is like this..
1,james
2,mathew
3,yancy
4,brandon
5,molner
6,nick
7,neil...and so on
How do I remove numbers and comas from this text file and keep only names?
Read the file into an array, where each array item is one line. Walk throught the array, find the first comma, and remove it and everything before. Then write it all back out again.
// Warning! Brain-compiled code ahead.
$arr = file('myfile.txt');
foreach ( $arr as &$val )
$val = substr($val, strpos($val, ',') + 1);
file_put_contents('myoutfile.txt', implode(PHP_EOL, $arr));
Note - no error checking. If a line lacks a comma, or comma is the last character, chaos ensues.
400k isn't incredibly much, so you should get away with this (tested):
foreach (file($path) as $line)
print preg_replace("~^[0-9]+\,(.*)~", "$1", $line);
Here is a perl one liner that do the job:
perl -i.save -pe 's/^\d+,//' test.txt
The original file will be saved in test.txt.save
This is tested and will return it as a list but you can save it to a database if you want:
$file_path='my_file.txt';
$file_handler = fopen($file_path, 'rt');
$doc = fread($file_handler, filesize($file_path)+1);
$rows = explode("\n", $doc);
$rows_array = array();
foreach ($rows as $row) {
$data = explode(",", $row);
$return_array[] = $data[1];
}
//print_r($return_array);
//you can save it to a db
echo '<ul>';
foreach($return_array as $value){
echo '<li>'.$value.'</li>';
}
echo '</ul>';