I have file with contents:
Tom TOM is a good student with excellent marks. This profile can be viewed online ar www.x.xom/tom/marks. He is one of the oustanding student
Tom1 TOM is a good student with excellent marks. This profile can be viewed online ar www.x.xom/tom/marks. He is one of the oustanding student
Tom2 TOM is a good student with excellent marks. This profile can be viewed online ar www.x.xom/tom/marks. He is one of the oustanding student
I have many lines like this.
Each line is having a name followed by a string untill end of the line.
I want to have a php script that will read first words keeps in a temp variable.
similarly I want all the text upto the end of the line to be going to the second variable.
I have used these
$myFile = "profiles.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, 5);
fclose($fh);
echo $theData;
but the issue with that is I am able to read the first 5 characters. I want to read first time only one word. second one is all string to be going to a variable.
Something like this?
$myFile = "profiles.txt";
$fh = fopen($myFile, 'r');
$anArray = array();
while (($line = fgets($fg)) !== false) {
list($name, $restOfLine) = explode(' ', $line, 2);
// Do something with $name and $restOfLine (e.g. put them in array)
$anArray[] = array('name' => $name, 'line' => $restofLine);
}
fclose($fh);
var_dump($anArray);
Related
i need to generate random sentences from dictionary. In dictionary is every word at one line, firstly i load this dictionary to array and after it i have a for cycle and randomly pickup some data, but if i wrote it, so it is at one line in browser, but in source code is every word at another line. Then I need to create a set of XML files from search engine and this new lines are indexed as /n/r and in XML source code it has got a symbol
So my question is how i can make a sentence which will be at one line in source code too. Thanks.
Here is piece of my code i donĀ“t have here randomly loading data, i only made it for illustration in for cycle.
$file = fopen("test.txt", "r");
$data = array();
while (($buffer = fgets($file)) !== false) {
$data[] = $buffer;
}
$sentence = '';
for ($i=0;$i<10;$i++){
$sentence = $sentence . $data[$i];
}
Use trim function to filter new line characters.
In your code use:
$data[] = trim($buffer);
I want to organize the texts in a file so if the texts in two or more lines are same, I want to keep them together.
Example:
US
CA
US
Would change to:
US
US
CA
I looked around, but couldn't find an answer really.
I can get the file to open with this:
$file = fopen("file.txt", "r");
while(!feof($file)){
$line = fgets($file);
// do same stuff with the $line
}
fclose($file);
Any help would be appreciated!
$pieces = explode(" ", $line);
// sorts it alphabetically
sort($pieces, SORT_NATURAL | SORT_FLAG_CASE);
Sounds like it should do the trick.
This script for write to a file from php :
<?php
$File = "YourFile.txt";
$Handle = fopen($File, 'a');
$Data = "Jane Doe\n";
fwrite($Handle, $Data);
$Data = "Bilbo Jones\n";
fwrite($Handle, $Data);
print "Data Added";
fclose($Handle);
?>
How to configure align the text like in fpdf (example : $pdf->Cell(20,10,'Title',1,1,'C');) ?
Use str_pad to get a decent result. Say you have 2 rows of data. Like:
Firstname, Lastname
If you want to sort these in a txt file to look nice you can do:
$firstname = "Jane";
$lastname = "Doe";
$firstname = str_pad($firstname, 30); //Force string to be 30 characters length
str_pad will add an amount of spaces to your string untill it reaches the length you want it to. Having all First names at exactly 30 characters will make the Last names appear perfectly sorted behind them as if you're having 2 lists in your TXT file.
Ofcourse if you ever want to read out the file you probebly want to trim away all those unnessary spaces. You can do this with:
$firstname = preg_replace('/\s+/', ' ', $firstname);
I am needing to turn a textfile into an array...I am not sure how to go about this because the other features ive seen for php take an entire file and put it into an array but not quite how I want it to be so I am looking for advice here..
The following is written in a textfile:
"jim kroi,richard wuu,yan kebler,justin persaud"
How can I use php to make an array where automatically a loop puts each name as an item of the array until all the names run out?
so the end result of what I am trying to do is:
$array= array("jim kroi","richard wuu","Yan kebler","justin persaud");
So a loop of some sort would basically search upto each comma and extract the name before it until all of the names run out....
There are some php substr and such functions but I cant quite think of how to do this..
Yes, I do have code, here it is:
<?php
error_reporting(-1);
$fp = fopen('numbers.csv', 'w');
fputcsv($fp, $_POST['names']);
fputcsv($fp, $_POST['numbers']);
fclose($fp);
?>
i put them all in a csv but now how can I make 2 arrays, one with name the other with numbers? http://imageshack.us/photo/my-images/215/csv.png/
using implode I get the error:
Warning: implode() [function.implode]: Bad arguments. in C:\Program Files\xampp\htdocs\xampp\something.php on line 14
<?php
error_reporting(-1);
$myFile = "testFile.txt";
$fh = fopen($myFile, 'r'); // open file
$theData = fread($fh, 5); // read file and store in var
$array = explode("\n", $theData); // explode string by lines using \n
echo implode("<br/>", $theData); // put the array back together and show each item as a line
fclose($fh);
?>
Try to use fgetcsv with a custom separator.
Something like:
$names = array_map('trim', explode(',', file_get_contents('%yourFileHere')));
Use the explode() function.
$string = "jim kroi,richard wuu,yan kebler,justin persaud";
$arrNames = explode(',', $string);
var_dump($arrNames);
see explode, read the file with file_get_contents
You can try explode;
$names = explode(',', $line);
http://php.net/manual/en/function.explode.php
it's simple
<?PHP
$myFile = "testFile.txt"; // file path and name
$fh = fopen($myFile, 'r'); // open file
$theData = fread($fh, 5); // read file and store in var
$array = explode("\n", $theData); // explode string by lines using \n
echo implode("<br/>", $theData); // put the array back together and show each item as a line
?>
I have the following notepad file;
dbName:
tableName:
numberOfFields:
I am trying to write a php app which assigns the value of dbName to $dbName, tableName to $tableName and numberOfFields to $numFields.
My code is:
$handle = #fopen("config.txt", "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
list($dbName, $tableName, $numFields) = explode(":", "$buffer");
}
fclose($handle);
}
however, ":" doesn't work as there are line breaks in between dbName and table Name. How do I explode $buffer, keeping the line breaks in the notepad file?
Thank you.
Have a look at the file function. It takes care of opening and reading the file, and returns an array of lines from the file. You could then iterate through the array and operate on each line individually.
http://us.php.net/manual/en/function.file.php
you can do this:
$data=file_get_contents("file");
$s = preg_split("/\n\n+/m", $data);
print_r($s);
You can use the chr($INT); function to look for the line break in your explode call.
Your can find more on the chr function here:
http://php.net/manual/en/function.chr.php
Add you can find the ascii chars for line break at:
http://www.asciitable.com/
fgets returns only one line. There is no way $buffer would ever have all three items at once, so that assignment to list() is wrong. For the first line, explode() will return an array with two items: "dbName" (text before the colon) and "" (text after the colon).
Does this work:
list ($dbName, $tableName, $numFields) = explode (':', implode ('', file ('config.txt')));
If you're sure of the line contents, and the file will not grow arbitrarily large:
1 <?php
2
3 $handle = #fopen("config.txt", "r");
4 if ($handle) {
5 $buffer = "";
6 while (!feof($handle)) {
7 $buffer = $buffer . trim(fgets($handle, 4096));
8 }
9 fclose($handle);
10
11 list($dbName, $tableName, $numFields) = explode(":", $buffer);
12 }
13
14 ?>
The while loop will go through all the lines and concatenate onto the same buffer after removing whitespace. This leaves just the content separated by ":". This is now amenable to explode.
As Nicolas wrote, feof gets one line at a time, so the list assignment needs to happen outside the loop.