<?php
$url="http://somedomain/something";
$lines = file('text.txt');
foreach ($lines as $line_num => $line) {
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
$dom = new DOMDocument;
$dom->loadHTMLFile($url.$line); //cannot load this
foreach ($dom->getElementsByTagName('p') as $node) {
// do stuff with $node
echo $node->nodeValue, "\n";
}
}
fclose($handle);
?>
I would like to attach the url to the lines of the text file:
Example
$url.$line1 (http://somedomain/something/line1)
$url.$line2 (http://somedomain/something/line2)
I have succesfully set up everything and the loop works well too. But whenever I try to concatenate the url with the line an HTTP 500 Error is returned
Also these characters '%0D%0A' attch themselves to end of the $url.$line and hence the error?
Any help appreciated
Update: The error is because of the '%0D%0A' characters somehow attaching themselves to the end of each url. Any idea how to remove them?
The extra characters you're getting, %0D%0A, are the carriage return and line feed characters for Windows computers. To get rid of them, you can use file with the FILE_IGNORE_NEW_LINES flag:
$lines = file('text.txt', FILE_IGNORE_NEW_LINES);
This will stop the carriage return characters being including on the end of each line in the array.
Alternatively, you can chomp the line ending with rtrim:
$line = rtrim($line);
Related
I have a large .txt file, within the .txt file, it contains the numbers 712 and other characters. example (712iu3 89234) or (712jnksuiosd). The characters after 712 will change, they may have spaces. I have a php script that reads the file line by line. I am trying to echo all characters after 712 If there are spaces I'd like to remove the spaces. I only need the first 20 characters excluding the spaces. So far I've tried
$file = new SplFileObject("1.txt");
// Loop until we reach the end of the file.
while (!$file->eof()) {
// Echo one line from the file.
echo $file->fgets();
}
// Unset the file to call __destruct(), closing the file handle.
$file = null;
try using the code below
<?php
$file = new SplFileObject("test.txt");
// Loop until we reach the end of the file.
while (!$file->eof()) {
$newString = str_replace(' ', '', $file->fgets());
if (strpos($newString, '712') !== false) {
$strWith712 = substr($newString, 0, 20);
$post = strripos($strWith712, '712');
$str = substr ($strWith712, $post );
echo $str;
}
}
$file = null;
?>
this replaces the white spaces and then searches for the string with the number '712' if a string is found then the letters after the number are printed
the strripos function is used to check the postiton of the string in a sentence from the last.
I am using PHP to read a simple text file with the fgets() command:
$file = fopen("filename.txt", "r") or exit('oops');
$data = "";
while(!feof($file)) {
$data .= fgets($file) . '<br>';
}
fclose($file);
The text file has leading white spaces before the first character of each line. The fgets() is not grabbing the white spaces. Any idea why? I made sure not to use trim() on the variable. I tried this, but the leading white spaces still don't appear:
$data = str_replace(" ", " ", $data);
Not sure where to go from here.
Thanks in advance,
Doug
UPDATE:
The text appears correctly if I dump it into a textarea but not if I ECHO it to the webpage.
Function fgets() grabs the whitespaces. I don't know what you are exactly doing with the $data variable, but if you simply display it on a HTML page then you won't see whitespaces. It's because HTML strips all whitespaces. Try this code to read and show your file content:
$file = fopen('file.txt', 'r') or exit('error');
$data = '';
while(!feof($file))
{
$data .= '<pre>' . fgets($file) . '</pre><br>';
}
fclose($file);
echo $data;
The PRE tag allows you to display $data without parsing it.
Try it with:
$data = preg_replace('/\s+/', ' ', $data);
fgets should not trim whitespaces.
Try to read the file using file_get_contents it is successfully reading the whitespace in the begining of the file.
$data = file_get_contents("xyz.txt");
$data = str_replace(" ","~",$data);
echo $data;
Hope this helps
I currently have the same requirement and experienced that some characters are written as a tab character.
What i did was:
$tabChar = ' ';
$regularChar = ' '
$file = fopen('/file.txt');
while($line = fgets($file)) {
$l = str_replace("\t", $tabChar, $line);
$l = str_replace(" ", $regularChar, $line);
// ...
// replacing can be done till it matches your needs
$lines .= $l; // maybe append <br /> if neccessary
}
$result = '<pre'> . $lines . '</pre>';
This one worked for me, maybe it helps you too :-).
I have a php program that looks at a log file and prints it to a page (code below). I don't want the user of said website to be able to look at any line containing a /. I know I could use trim to delete certain characters, but is there a way to delete the entire line? For example, I want to keep something like "Hello" and delete something like /xx.xx.xx.xx connected. All the lines I wish to delete have the same common key, /. Peoples names in said log file have <>s around them, so I must use htmlspecialcharacters
$file = file_get_contents('/path/to/log', true);
$file = htmlspecialchars($file);
echo nl2br($file);
Thanks for your help!
EDIT:
Thanks for all of the answers, currently tinkering with them!
EDIT2:
final code:
<?php
$file = file_get_contents('/path/to/log', true);
// Separate by line
$lines = explode(PHP_EOL, $file);
foreach ($lines as $line) {
if (strpos($line, '/') === false) {
$line = htmlspecialchars($line . "\n");
echo nl2br($line);
}
}
?>
Do you mean, like this?
$file = file_get_contents('/path/to/log', true);
// Separate by line
$lines = explode(PHP_EOL, $file);
foreach ($lines as $line) {
if (strpos($line, '/') === false) {
// If the line doesn't contain a "/", echo it
echo $line . PHP_EOL;
}
}
For anyone wondering, PHP_EOL is the PHP constant for "end of line" and promotes consistency between different systems (Windows, UNIX, etc.).
If you are iterating through the file line by line you can check with preg_match if the line contains /character and skip the echo if it does. If not, first split them at new line and iterate over that array.
If you don't want to split the file you can probably use preg_replace with a regexp such as (^|\n).*/.*(\n|$) and replace with empty string.
Use the str_replace function -
http://php.net/manual/en/function.str-replace.php. Alternate solution (before escaping the special characters) -
/* pattern /\/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\sconnected/ = /xx.xx.xx.xx connected */
/* pattern will be replaced with "newtext" */
$file = file_get_contents("/path/to/log", true);
$lines = explode("\n", $file);
foreach ($lines as $line)
$correctline = preg_replace( '/\/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\sconnected/', 'newtext', $line );
echo $correctline;
}
<?php
$file = file_get_contents("/path/to/log", true);
$lines = explode("\n", $file);
foreach ($lines AS $num => $line)
{
if ( strpos($line, "/") === false ) // Line doesn't contain "/"
{
echo htmlspecialchars($line) . "\n";
}
}
?>
Ok I have a data similar like this, written in a single text file..
BOX,12
CAN,99
.... and so on.
Now I want to execute those into mysql tables using explode() what will not be a problem.
But anyway, any ideas how can I grab row per row (line per line) from a text file into PHP?
Read it like this:
$lines = file('my/file/text.txt');
foreach ($lines as $line_num => $line) {
echo "Line #<b>{$line_num}</b> : " . $line . "<br>\n";
}
You can do this:
// Trying to open TXT file
if( $file = fopen('file.txt', 'r') )
{
// Loop until the End Of File
while( ! feof($file) )
{
// Get current line
$line = fgets($file);
echo $line . '<br />';
}
// Closing TXT file
fclose($file);
}
else
echo 'fopen() fail';
For more detais about the functions:
fopen()
feof()
fgets()
fclose()
I've got a problem where I'm trying to read a text file like this:
Joe
Johnson
Linus
Tourvalds
and while parsing it in php, I need to be able to detect the newlines. I'm trying to correctly define $newline. I'm looping through the array of lines in the $file variable.
while($line = next($file))
if($line = $newline)
echo "new line";
The problem is that I can't seem to match the newline character. I know that it is actually showing up in the $file array, because this:
while($line = next($file))
echo $line;
outputs the file verbatim, with newlines and all. I've already tried "\n", " ", and I'm not sure what to try next. A little help?
$file = file("path/to/file.txt");
// Incase you need to call it multiple times ...
function isNewLine($line) {
return !strlen(trim($line));
}
foreach ($file as $line) {
if (isNewLine($line)) {
echo "new line<br/>";
}
}
Maybe something like this would work for you?
while($line = next($file)) {
if(in_array($line, array("\r", "\n", "\r\n"))) {
echo "new line";
}
}
I think this solution may help you guys. This works if you are parsing csv that is generated from Mac or windows. Reading csv with multilines created in Mac, gives problem i.e. you cannot read each line in a loop but all csv data is read as single line.
This problem is solved by following solution:
//My CSV contains only one column
$fileHandle = fopen("test.csv",'r');
$codesArray = array();
count = 0;
while (!feof($fileHandle) ) {
$line = fgetcsv($fileHandle);
if($line[0]!="") {
$data = str_replace("'", "", (nl2br ($line[0])));
$dataArray = explode('<br />' ,$data );
foreach($dataArray as $data) {
$codesArray[] = trim($data);
}
}
}
echo "<pre>";
print_r($codesArray);