Im parsing a csv file to get certain fields and modify them but the problem is that for some reason the feof only iterates once. I did some testing and I realized that if I remove the fgetcsv line the file is read until the end of file. Herebelow is my code. Any help would be greatly appreciated.
<?php
include 'property-features.php';
//------------- get lat and long --------------//
function geocode($address){
// url encode the address
$address = urlencode($address);
$json = file_get_contents('http://open.mapquestapi.com/geocoding/v1/address?key={mykey}&location='.$address);
$jsonArr = json_decode($json);
$lat1 = $jsonArr->results[0]->locations[0]->latLng->lat;
$lon1 = $jsonArr->results[0]->locations[0]->latLng->lng;
// verify if data is complete
if($lat1 && $lon1){
// put the data in the array
$data_arr = array();
array_push(
$data_arr,
$lat1,
$lon1,
$address
);
return $data_arr;
}else{
return false;
}
}
/* ------------- fix property address --------------- */
function ordinal($num) {
$ones = $num % 10;
$tens = floor($num / 10) % 10;
if ($tens == 1) {
$suff = "th";
} else {
switch ($ones) {
case 1 : $suff = "st"; break;
case 2 : $suff = "nd"; break;
case 3 : $suff = "rd"; break;
default : $suff = "th";
}
}
return $num . $suff;
}
/* ------------------ Open original mls feed csv and create a csv file ------------------*/
$file_handle = fopen("sefl_data.csv", "r");
$file = fopen("/home/javy1103/public_html/wp-content/uploads/wpallimport/files/mlsFeed.csv", "w");
while (!feof($file_handle)) {
echo "string";
$line_of_text = fgetcsv($file_handle);
$photos = intval($line_of_text[88]);
if(1 == 1){
/* ------------------ get parking spaces ------------------*/
$line_of_text[84] = str_replace($patterns, $replacement, $line_of_text[84]);
if(substr_count($line_of_text[84], "1 parking") || substr_count($line_of_text[84], "1 car garage")){
$line_of_text[95] = 1;
}else if (substr_count($line_of_text[84], "2 parking") || substr_count($line_of_text[84], "2 car garage")){
$line_of_text[95] = 2;
}else if (substr_count($line_of_text[84], "3 parking") || substr_count($line_of_text[84], "3 car garage")){
$line_of_text[95] = 3;
}else if(substr_count($line_of_text[84], "3 parking or more parking") || substr_count($line_of_text[84], "3 or more car")) {
$line_of_text[95] = "3+";
}
/* ---------------- Get latitude and longitude -------------------*/
if(!empty($line_of_text[23])){
$stNum = preg_replace("/[^0-9]/","",$line_of_text[21]);
echo $stNum.'<BR>';
$address = $line_of_text[20].' '.$line_of_text[23].' '.$line_of_text[21].','.$line_of_text[27].',FL,'.$line_of_text[29];
}else {$address = $line_of_text[20].' '.$line_of_text[21].','.$line_of_text[27].',FL,'.$line_of_text[29];}
//$latLong = geocode($address);
//$line_of_text[25] = $latLong[0].', '.$latLong[1];
$line_of_text[96] = "";
$counter = 2;
//unset($line);
$url = $line_of_text[89];
//$line[0] = $url;
while ($counter <= $photos && $counter < 15) {
$photoNumber = '_'.($counter).'.jpg';
$line_of_text[96+$counter] = substr_replace($url, $photoNumber, sizeof($url) - 5, sizeof($photos)+4);
$counter++;
}
}
}
fclose($file_handle);
fclose($file);
?>
Hazarding a guess, and quoting from the PHP docs
Note: If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem.
Related
I am trying to remove certain line set based on ipaddress in large text file having approx. 60,000 lines. Each line set starting from MaxBytes[ipaddress] and ending with </TABLE> and a blank line present between each line set. There is variation in table lines in text file.
Sample line set :
MaxBytes[192.168.1.1]: 10000 <--start line
<TABLE>
<TR><TD>IP Address:</TD><TD>192.168.1.1</TD></TR>
<TR><TD>Max Speed:</TD> <TD>300</TD></TR>
</TABLE> <-- end line (Need to delete lines from start to end line)
I am trying to find start line using below codes (supported by Yerke) but unable to find out way to find next line number containing </table> tag. I need to find out start and end line number of line set containing specific ipaddress and delete it.
I am a beginner in coding, so I might need extended guidance.
codes :
<?php
$dir = "example.txt";
$searchstrt = "192.168.1.1";
///// find details
function find_line_number_by_string($dir, $searchstrt, $case_sensitive=false ) {
$line_number = [];
if ($file_handler = fopen($dir, "r")) {
$i = 0;
while ($line = fgets($file_handler)) {
$i++;
//case sensitive is false by default
if($case_sensitive == false) {
$searchstrt = strtolower($searchstrt);
$line = strtolower($line);
}
//find the string and store it in an array
if(strpos($line, $searchstrt) !== false){
$line_number[] = $i;
}
}
fclose($file_handler);
}else{
return "File not exists, Please check the file path or dir";
}
return $line_number;
}
$line_number = find_line_number_by_string($dir, $searchstrt);
var_dump($line_number);
?>
Sample example.txt
MaxBytes[192.168.1.1]: 10000
<TABLE>
<TR><TD>IP Address:</TD><TD>192.168.1.1</TD></TR>
<TR><TD>Max Speed:</TD> <TD>300</TD></TR>
</TABLE>
MaxBytes[192.168.1.2]: 30000
<TABLE>
<TR><TD>IP Address:</TD><TD>192.168.1.1</TD></TR>
<TR><TD>Max Speed:</TD> <TD>300</TD></TR>
<TR><TD>Name:</TD> <TD>ABC</TD></TR>
</TABLE>
MaxBytes[192.168.1.3]: 10000
<TABLE>
<TR><TD>IP Address:</TD><TD>192.168.1.1</TD></TR>
<TR><TD>Max Speed:</TD> <TD>200</TD></TR>
<TR><TD>Location:</TD> <TD>INDIA</TD></TR>
</TABLE>
I found some workaround to get line numbers of line set containing desired ip address. Does anyone suggest better way to do it.
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
$dir = "example.txt";
$searchstrt = "192.168.1.2";
$searchend = "</TABLE>";
///// find details
function find_line_number_by_string($dir, $searchstrt, $case_sensitive=false ) {
$line_number = [];
if ($file_handler = fopen($dir, "r")) {
$i = 0;
while ($line = fgets($file_handler)) {
$i++;
//case sensitive is false by default
if($case_sensitive == false) {
$searchstrt = strtolower($searchstrt);
$line = strtolower($line);
}
//find the string and store it in an array
if(strpos($line, $searchstrt) !== false){
$line_number[] = $i;
}
}
fclose($file_handler);
}else{
return "File not exists, Please check the file path or dir";
}
return $line_number;
}
$line_number = find_line_number_by_string($dir, $searchstrt);
//var_dump($line_number);
$start = $line_number[0];
////////////////////////
function find_line_number_by_string1($dir, $searchend, $case_sensitive=false, $start) {
$line_number1 = [];
if ($file_handler1 = fopen($dir, "r")) {
$i = $start;
// $i = 0;
while ($line1 = fgets($file_handler1)) {
$i++;
//case sensitive is false by default
if($case_sensitive == false) {
$searchend = strtolower($searchend);
$line1 = strtolower($line1);
}
//find the string and store it in an array
if(strpos($line1, $searchend) !== false){
$line_number1[] = $i;
}
}
fclose($file_handler1);
}else{
return "File not exists, Please check the file path or dir";
}
return $line_number1;
}
$line_number1 = find_line_number_by_string1($dir, $searchend, $case_sensitive=false, $start);
$first = $line_number[0];
$last = $line_number1[0];
//var_dump($line_number1);
for ($x = $first; $x <= $last; $x++) {
echo "Line number to be delete : $x <br>";
}
?>
I found solution of my question. I have just added few more line in my existing code. Now its working fine as required.
$lines = file($dir, FILE_IGNORE_NEW_LINES);
for ($x = $first; $x <= $last; $x++) {
echo "Line number to be delete : $x <br>";
$lines[$x] = '';
unset($lines[$x]);
}
//var_dump($lines);
file_put_contents($dir , implode("\n", $lines));
I'm writing a script for download from FTP..
In the form I need to show files and folders..
With ftp_nlist, they come all togethers but I want to know who's who ..
I can't find an easy way to do this:
$contents = ftp_nlist($connection, $rep);
$dossiers =array();
$fichiers = array();
foreach($contents as $content){
//if folder
if (is_folder($content)) $dossiers[] = $content;
//si file
if(is_filex($content)) $fichiers[] = $content;
}
Of course is_file and is_dir don't work with distant files...
I've find something with ftp_rawlist and the size of each result..
like this:
if($result['size']== 0){ //is dir }
But in case of an empty file???
So what id the way to know what is a folder and what is a file??
Thanks!
I've had the same problem and this was my solution:
$conn = ftp_connect('my_ftp_host');
ftp_login($conn, 'my_user', 'my_password');
$path = '/';
// Get lists
$nlist = ftp_nlist($conn, $path);
$rawlist = ftp_rawlist($conn, $path);
$ftp_dirs = array();
for ($i = 0; $i < count($nlist) - 1; $i++)
{
if($rawlist[$i][0] == 'd')
{
$ftp_dirs[] = $nlist[$i];
}
}
I know the above code could be optimised and do just one FTP request instead of two but for my purposes this did the work.
For anyone looking for a cleaner solution, I've found a script to parse ftp_rawlist in this LINK:
Function
function parse_ftp_rawlist($List, $Win = FALSE)
{
$Output = array();
$i = 0;
if ($Win) {
foreach ($List as $Current) {
ereg('([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|) +(.+)', $Current, $Split);
if (is_array($Split)) {
if ($Split[3] < 70) {
$Split[3] += 2000;
}
else {
$Split[3] += 1900;
}
$Output[$i]['isdir'] = ($Split[7] == '');
$Output[$i]['size'] = $Split[7];
$Output[$i]['month'] = $Split[1];
$Output[$i]['day'] = $Split[2];
$Output[$i]['time/year'] = $Split[3];
$Output[$i]['name'] = $Split[8];
$i++;
}
}
return !empty($Output) ? $Output : false;
}
else {
foreach ($List as $Current) {
$Split = preg_split('[ ]', $Current, 9, PREG_SPLIT_NO_EMPTY);
if ($Split[0] != 'total') {
$Output[$i]['isdir'] = ($Split[0] {0} === 'd');
$Output[$i]['perms'] = $Split[0];
$Output[$i]['number'] = $Split[1];
$Output[$i]['owner'] = $Split[2];
$Output[$i]['group'] = $Split[3];
$Output[$i]['size'] = $Split[4];
$Output[$i]['month'] = $Split[5];
$Output[$i]['day'] = $Split[6];
$Output[$i]['time/year'] = $Split[7];
$Output[$i]['name'] = $Split[8];
$i++;
}
}
return !empty($Output) ? $Output : FALSE;
}
}
Usage
// connect to ftp server
$res_ftp_stream = ftp_connect('my_server_ip');
// login with username/password
$login_result = ftp_login($res_ftp_stream, 'my_user_name', 'my_password');
// get the file list for curent directory
$buff = ftp_rawlist($res_ftp_stream, '/');
// parse ftp_rawlist output
$result = parse_ftp_rawlist($buff, false);
// dump result
var_dump($result);
// close ftp connection
ftp_close($res_ftp_stream);
I have text file
name,name1
willhaveishere1
name,name2
willhaveishere2
name,name3
willhaveishere3
i want read it and return like that
$nn = name1
$ss = willhaveishere1
with my code i get only name1
my code is
$file1 = "file.txt";
$file = file($file1);
$count = count($file);
if($count > 0) {
$i = 1;
foreach($file as $row) {
$n = strstr($row, 'name,');
$cc = array("name,");
$dd = array("");
$nn = str_replace($cc, $dd, $n);
echo $nn;
$i++; } }
This is probably what you need
if($count > 0) {
foreach($file as $row) {
$pos = strpos($row, ',');
if($pos !== false){
echo substr($row, $pos + 1);
$nn[] = substr($row, $pos + 1);
} else {
echo $row;
$ss[] = $row;
}
}
}
EDIT
Yes, just loop through, but make sure both $nn and $ss has same count, which is depending on your file.
Also Note: mysql_* functions has been deprecated, so please use mysqli or PDO instead
$count = count($nn);
for($i=0; $i < $count; $i++){
$sql = "INSERT INTO users(name, line) VALUES('$nn[$i]', '$ss[$i]')"; mysql_query($sql);
}
EDIT 2
try this example:
$file = array(
0 => 'name,name1',
1 => 'willhaveishere1',
2 => 'name,name2',
3 => 'willhaveishere2',
4 => 'name,name3',
5 => 'willhaveishere3'
);
$count = count($file);
if($count > 0) {
foreach($file as $row) {
$pos = strpos($row, ',');
if($pos !== false){
$nn[] = substr($row, $pos + 1);
} else {
$ss[] = $row;
}
}
}
echo '<pre>';
$count = count($nn);
for($i=0; $i < $count; $i++){
$sql = "INSERT INTO users(name, line) VALUES('$nn[$i]', '$ss[$i]');";
echo $sql.PHP_EOL;
}
You can try this straightforward method:
if($fh = fopen("file.txt","r")){
$nameBefore = false;
//loop through every line of your file
while (!feof($fh)){
$line = fgets($fh);
//check if the name was detected in previous line
if ($nameBefore !== false)
{
//you have the set of name and line, do what you want
echo $nameBefore . ': ' . $line . '<br />';
$nameBefore = false;
}
else
{
//see if the line is made of two coma separated segments and the first one is 'name'
//Remember the name for the next line
$parts = explode(',', $line);
if (count($parts) == 2 && $parts[0] == 'name')
$nameBefore = $parts[1];
}
}
fclose($fh);
}
One option is to use strpos to find the first occurrence of the character in the line, and if found remove everything from the line before that position. This way you are left with only the part of the line you are interested in.
Code:
$character = ',';
$fileHandle = fopen('file.txt', 'r');
while (!feof($fileHandle)) {
// Retrieve the line from the file
$line = fgets($fileHandle);
// If the line contains the character
// Remove everything before the character
$charPos = strpos($line, $character);
if ($charPos !== false) {
$line = substr($line, $charPos + 1);
}
// Do something with the remainder of the line
echo $line . PHP_EOL;
}
fclose($fileHandle);
Output:
name1
willhaveishere1
name2
willhaveishere2
name3
willhaveishere3
If you wish to retrieve the following line, simply do another retrieve line call in your loop:
while (!feof($fileHandle)) {
// Retrieve two lines in one loop iteration
$lineOne = fgets($fileHandle);
$lineTwo = fgets($fileHandle);
}
Making sure to only apply the comma replace part on the first line. This can lead to problems though if your data is... inconsistent.
Hope this helps.
I am trying to search a line in a text file and then print the following three lines. For example, if the text file has
1413X
Peter
858-909-9999
123 Apple road
then my PHP file would take in an ID ("1413X") through a form, compare it to lines in the text file - essentially a mock database - and then echo the following three lines. Currently, it is echoing only the phone number (with the second half of the numbers wrong??). Thanks for your help.
<?php
include 'SearchAddrForm.html';
$file = fopen("addrbook.txt", "a+");
$status = false;
$data = '';
if (isset($_POST['UserID']))
{
$iD = $_POST['UserID'];
$contact = "";
rewind($file);
while(!feof($file))
{
if (fgets($file) == $iD)
{
$contact = fgets($file);
$contact += fgets($file);
$contact += fgets($file);
break;
}
}
echo $contact;
}
fclose($file);
?>
It is better to set some flag that you found id and some counter to count lines after it to achieve your aim.
<?php
include 'SearchAddrForm.html';
// $file = fopen("addrbook.txt", "a+");
$file = fopen("addrbook.txt", "r");
$status = false;
$data = '';
if (isset($_POST['UserID']))
{
$iD = $_POST['UserID'];
$contact = "";
rewind($file);
$found = false;
$count = 1;
while (($line = fgets($file)) !== FALSE)
{
if ($count == 3) // you read lines you needed after you found id
break;
if ($found == true)
{
$contact .= $line;
$count++
}
if (trim($line) == $iD)
{
$found = true;
$contact = $line;
}
}
echo $contact;
}
fclose($file);
?>
This kind of example how you can achieve this. And as you see in comment you should use $contact .= value, not $contact += value.
Also instead of reading you can take the whole file in array line by line using function file.
And why are opening file for writing?
What I did:
<?php
//input (string)
$file = "before\n1413X\nPeter\n858-909-9999\n123 Apple road\nafter";
//sorry for the name, couldn't find better
//we give 2 strings to the function: the text we search ($search) and the file ($string)
function returnNextThreeLines($search, $string) {
//didn't do any check to see if the variables are not empty, strings, etc
//turns the string into an array which contains each lines
$array = explode("\n", $string);
foreach ($array as $key => $value) {
//if the text of the line is the one we search
//and if the array contains 3 or more lines after the actual one
if($value == $search AND count($array) >= $key + 3) {
//we return an array containing the next 3 lines
return [
$array[$key + 1],
$array[$key + 2],
$array[$key + 3]
];
}
}
}
//we call the function and show its result
var_dump(returnNextThreeLines('1413X', $file));
Again I'm working on a working CSV filter. It will search through about 500 lines of promotional code and return its amount to ajax receiver. The weird thing is, if I only enter 2 letters, instead of searching for exact fit, the php processor would return the result once it has found a value which contains my entered letters! I need it to look for only exact fit of 4-strings value.
Here's my code so far:
<?php
// if data are received via POST, with index of 'test'
if (isset($_POST['test'])) {
$promocodevalid = false;
$file = fopen('test.csv', 'r');
$coupon = array($_POST['test']);
$coupondef = $_POST['test']; // get data
$coupon = array_map('preg_quote', $coupon);
$regex = '/'.implode('|', $coupon).'/i';
while (($line = fgetcsv($file)) !== FALSE) {
list($promocode, $amount) = $line;
if(preg_match($regex, $promocode)) {
$validity = 1;
echo $amount."[BRK]".$promocode."[BRK]".$validity;
$promocodevalid = true;
break;
}
}
if(!$promocodevalid) {
$validity = 0;
echo $amount."[BRK]".$promocode."[BRK]".$validity;
}
}
?>
Try to avoid regexes where they are not needed. Search for str* function you need.
Above code should look like:
if (isset($_POST['test'])) {
$promocodevalid = false;
$file = fopen('test.csv', 'r');
$coupondef = $_POST['test']; // get data
while (($line = fgetcsv($file)) !== FALSE) {
list($promocode, $amount) = $line;
// remove strtolower if you are have lowercase promocode,
// but probably leave a $coupondef lowered.
if(strpos(strtolower($promocode), strtolower($coupondef)) === 0) {
$validity = 1;
echo $amount."[BRK]".$promocode."[BRK]".$validity;
$promocodevalid = true;
break;
}
}
if(!$promocodevalid) {
$validity = 0;
echo $amount."[BRK]".$promocode."[BRK]".$validity;
}
}