So I have a sql database that generates an xml file. This xml file gets put on a webserver and then parsed by a php file. Unfortunately randomly sometimes there will be a line break in the xml file that causes it to be unable to be parsed.
http://imgur.com/jNgiE
As you can see from line 12-14. There is a linebreak. But I have no idea why. And I even wrote a script to remove carriage returns and newline characters but the linebreaks still remain. Anyone have any ideas?
$inputXML = file_get_contents("ukso.xml");
$fixedXML = str_replace("\r","",$inputXML);
$fixedXML = str_replace("\n","",$inputXML);
$fixedXML = str_replace(" ","",$inputXML);
$myFile = "ukso.xml";
print $fixedXML;
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $fixedXML);
fclose($fh);
Your code to remove linebreaks is broken. make the following changes:
$inputXML = file_get_contents("ukso.xml");
$fixedXML = str_replace("\r","",$inputXML);
$fixedXML = str_replace("\n","",$fixedXML); // note: reference the correct variable here
$fixedXML = str_replace(" ","",$fixedXML); // and here.
$myFile = "ukso.xml";
print $fixedXML;
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $fixedXML);
fclose($fh);
The code as you have it is only trimming out double spaces.
I see you're using notepad++
Go into view-show symbol-show all characters. That will tell you exactly what characters there are in the file.
It's possible that the line breaks are also due to the line being too long otherwise. XML files are usually designed to have line breaks at the end of tags, and you may be running into a situation where the line length is too long somewhere in one of the programs. (Often they create a fixed buffer and read in as much as possible, and if it's too long the program will chop the line)
Related
I have a very large file that consists of a single string. Because of the size of the file, I do not want to read the entire string into memory.
The last character will always be a closing bracket ] as this string is a json array. I want to insert a small json object (represented as a string) immediately before that closing bracket. I have seen a few ideas, but cannot get anything to work.
As you can see, I am trying to open the file and use fseek to move the file pointer to just in front of the ]. Then I try to write the new string into the existing string at that position.
However, the effect of this is simply to append the new string to the end of the existing string, which is not what I want.
As a simplified example, let's say the file starts out containing this string:
[{"name":"alice","city":"london"}]
And then I want to add a second person to this list using this code:
$new_person = ",{\"name\":\"bob\",\"city\":\"paris\"}";
$filename = "people.json";
$fh = fopen($filename, "a+");
$filesize = filesize($filename);
$stat = fstat($fh);
fseek($fh, $stat[$filesize]-1);
fwrite($fh, $new_person);
fclose($fh);
But what I wind up with is a file that contains this string:
[{"name":"alice","city":"london"}],{"name":"bob","city":"paris"}
My PHP skills are terrible. I can't tell if my fseek is pointing to the wrong spot or if the issue is elsewhere. Thanks for any help.
From the docs (emphasis mine):
a+: Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() only affects the reading position, writes are always appended.
Use r+ mode instead, and instead of fstat you can do:
fseek($fh, -1, SEEK_END);
Please try the following code to get your solution, i have tested and its work fine...
try{
$new_person = ",{\"name\":\"bob\",\"city\":\"paris\"}]";
$filename = "people.json";
$fh = fopen($filename, "a+");
$stat = fstat($fh);
ftruncate($fh, $stat['size'] - 2);
fwrite($fh, $new_person);
fclose($fh);
}catch(Exception $exc){
echo($exc->getMessage());
}
I have the following test script:
<?php
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Floppy Jalopy\n";
fwrite($fh, $stringData);
$stringData = "Pointy Pinto\n";
fwrite($fh, $stringData);
fclose($fh);
?>
when run however and opened usign Notepad, the data is returned in a single line without breaks as:
Floppy Jalopy(crazy box)Pointy
Pinto(crazy box)
where i cant find the appropriate character for 'crazy box' but its a REALLY crazy box. WHAT GIVES!
It is best to use PHP_EOL. This is cross-platform, so it automatically chooses the correct newline character(s) for the platform PHP is currently running on.
$stringData = "Floppy Jalopy" . PHP_EOL;
PHP Constants
If you want to open the file in Windows notepad, you must use Windows line breaks: \r\n
Your code runs fine.
Use Notepad2 or Notepad++ if you're working on Windows. The built-in Notepad is unable to cope with Unix-style line endings.
. PHP_EOL; will work universally
I have the following test script:
<?php
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Floppy Jalopy\n";
fwrite($fh, $stringData);
$stringData = "Pointy Pinto\n";
fwrite($fh, $stringData);
fclose($fh);
?>
when run however and opened usign Notepad, the data is returned in a single line without breaks as:
Floppy Jalopy(crazy box)Pointy
Pinto(crazy box)
where i cant find the appropriate character for 'crazy box' but its a REALLY crazy box. WHAT GIVES!
It is best to use PHP_EOL. This is cross-platform, so it automatically chooses the correct newline character(s) for the platform PHP is currently running on.
$stringData = "Floppy Jalopy" . PHP_EOL;
PHP Constants
If you want to open the file in Windows notepad, you must use Windows line breaks: \r\n
Your code runs fine.
Use Notepad2 or Notepad++ if you're working on Windows. The built-in Notepad is unable to cope with Unix-style line endings.
. PHP_EOL; will work universally
I can't tell you how stupid I feel having to ask this question, but I've been working on the most simple of commands (for two days) and can't find the problem. I 'borrowed' some code for a non repeating hit counter. I have tried to get it to work and I finally determined I'm not getting access to the simple txt files that store the hits or the one that stores the ip addresses. I've read the problems here, looked at the command in a 'Dummies' book and even watched YouTube videos and I'm blind to the problem. I've tried using a string for the file name and using the filename directly. I had the files in a sub folder on the server and thought that might be the issue so I moved them to the root with the same error. If someone can see why this isn't working I'd be eternally grateful.
This is only part of the whole code but it's where I determined that it fails.
$filename = 'countfix.txt';
$handle = fopen('$filename', 'r');
fread($handle, $current_inc)
or die ("Can't open file");
echo $current_inc;
fclose($handle);
Thanks.
This is wrong:
$handle = fopen('$filename', 'r'); // tries to open a file named $filename
It should be written this way:
$handle = fopen($filename, 'r'); // no quotes, opens countfix.txt
You might have meant to write this instead:
$handle = fopen("$filename", 'r');
wherein the double quotes will cause the real value of $filename to be substituted into the string (thus making the code work), but there is no point in doing that. Lose the quotes.
Additionally, this code doesn't do what it says:
fread($handle, $current_inc) or die ("Can't open file");
Here the error message is printed if you cannot read from the file, not when you fail to open it. You should check the return value of fopen instead or modify the message to be more accurate.
This is the right way to do it:
$handle = fopen("$filename", 'r');
You must enclose variables with doubble quotes, or not enclose them at all! This is how PHP is.
You might want to read this: what is the difference between single quoted and double quoted strings in php
Hi I am new to PHP and hope someone can help me.
Sometimes the output from the executed shell script can be hundreds of lines long, the lines are separated with a <BR> (replaced \n in shell script) for formatted html output.
So I need to know how to make the output paginated, I looked at some other similar solutions here but I couldn't make them work as they did different things.
$url = $_POST["website"];
$safeurl = escapeshellarg($url);
#passthru("./check -n ".$safeurl);
$stuff=shell_exec("./webcheck -n ".$safeurl);
$webFile = ($url.'.txt');
$write = $stuff;
$fh = fopen($webFile, 'w') or die("can't open file");
fwrite($fh, $write);
fclose($fh);
$fh = fopen($webFile, "r") or die("can't open file");
$frstuff=fread($fh, filesize($webFile));
fclose($fh);
echo $frstuff;
If you try using exec with an additional parameter instead of shell_exec, you can get the output lines as an array rather than one long string.
$output = array();
exec("./webcheck -n $safeurl", $output);
// Inspect the contents of $output
var_dump($output);
Then you can iterate through that array ($output) as needed.
Its not the best solution, but the easiest is going to be using javascript to do the pagination.
Try: http://plugins.jquery.com/project/pagination
Otherwise you could plugin a generic database pagination script and change the database adaptor to point to a file adaptor, where number of rows becomes number of lines.
Edit: provided better link