PHP writing file with wrong line endings - javascript

I have some javascript code that generates a file, which then needs to be saved server side for further processing by a Python script.
My javascript creates a string, and in that string I'm using '\n' to break the lines, then I feed that string into a PHP file for saving as a text file.
my issue is when I open the file in vi, it shows ^M at the end of each line, which confuses python. I've tried getting python to generate a substring of the line without the ^M at the end, but Python won't do that.
so my javascript string looks like this when I debug:
line1
line2
line3
but the saved file looks like this:
line1^M
line2^M
line3^M
this is my javascript:
for(var i = 0;i < existingList.length;i++){
console.log(existingList.options[i].value);
saveString += existingList.options[i].value + '\n';
}
saveToFile(saveString);
function saveToFile(saveString){
var data = new FormData();
data.append("data" , saveString);
var xhr = new XMLHttpRequest();
xhr.open( 'post', 'savefile.php', true );
xhr.send(data);
}
This is my PHP:
<?php
if(!empty($_POST['data'])){
$data = $_POST['data'];
$fname = "Cards.txt";
$file = fopen($fname, 'w');//creates new file
fwrite($file, $data);
fclose($file);
}
?>
I've tried converting to UTF8, I've tried String.fromCharCode(13) instead of '\n' and I've tried removing the ^M in Python... any ideas?
SOLUTION:
In python I was using
lines = text_file.read().split('\r')
or:
lines = text_file.read().split('\n')
I found if you use splitlines, it automatically handles the line ending:
lines = text_file.read().splitlines()

use double quotes for new line "\n" instead of '\n'

Try to replace your line(s) of code:
saveString += existingList.options[i].value + '\n';
With:
saveString += existingList.options[i].value + "\n";
OR with:
saveString += existingList.options[i].value + "\r\n";
You can use "\n", "\r\n" only as using single quotes wont work..
You can do it by editing the file or the content with php before you use the file, replace the content you don't want with nothing and save the file using file_get_contents() and file_put_contents(), str_replace() this is not pyton but you might want to try to fix it with php as you can do it before you use the file..
There are multiple ways to do this, this is..
str_replace() example:
<?php
/** More information regarding file_get_contents() - http://php.net/manual/en/function.file-get-contents.php **/
$End_Of_Line = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/path/to/Cards.txt');
/** More information regarding str-replace() - http://php.net/manual/en/function.str-replace.php **/
$End_Of_Line = str_replace('^M', '', $End_Of_Line);
/** Or use $End_Of_Line = str_replace('^M', "\r\n", $End_Of_Line); if you need new lines.. **/
/** More information regarding file_put_contents() - http://php.net/manual/en/function.file-put-contents.php **/
file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/path/to/Cards.txt', $End_Of_Line);
?>
I hope this will bring you into the right direction

I tested jagb's solution, and neither of them seemed to work but it got me thinking about how python reads the file. I finally found the answer:
In python I was using
lines = text_file.read().split('\r')
or:
lines = text_file.read().split('\n')
I found if you use splitlines, it automatically handles the line ending:
lines = text_file.read().splitlines()

Related

adding php stops my javascript init() working

I have a file written in html and javascript. I have an init function that I used to initialise some variables in javascript. To make this happen I have the following tag in the body statement:
<body onload="init()">
and then later I have the function itself. All seems to work well.
function init() {
// initialisation stuff here
var arr = ["happy", "winter", "hot", "cold", "male", "female", "summer", "spring", "autumn"];
document.getElementById('word').innerHTML = arr[Math.floor(Math.random() * arr.length)];
flag = 0;
}
However, I now want to load some data from a file so that I can replace the arr variable in my init() function with a much longer list of words. So just below the body statement I put the following:
<?php
$words = file('data.txt');
?>
I changed the file extension from html to php so that the php script would run before the page loads. When I change the extension to php it works fine. But as soon as I add those php lines I get an error. When I look using Develops Tools it says - Uncaught Reference Error: Init not defined.
But my init function is still there. Why would adding those php lines affect the javascript init function?
EDIT
I had the following two lines in my init function
// var words = <?php echo '["' . implode('", "', $words) . '"]' ?>;
// alert(words);
I thought they were commented out. But obviously not. If I remove them the code works fine.
However, although it is evident from the run-time inspection that
var words = <?php echo '["' . implode('", "', $words) . '"]' ?>;
alert(words);
is bringing the data into the init function, somehow it is still producing an error.

How to create an Image object with a base-64 source?

I have a remote web page, on which I use CURL and DOMDocument to retrive the src attribute of an image.
I use the following code, mixing JS in a PHP echo
<?php
$toEcho = "";
$toEcho .= // some stuff here...
$toEcho.="
<script>
//I create the object
var img = new Image();
//I set the source with the PHP variable : I get an error because of unexpected token ":"
img.src = $imgsrc;
</script>"
echo $toEcho;
?>
I checked the source, it is valid, and displays properly in my browser(s).
Where could this come from ?
Set the source like this:
img.src = '$imgsrc';

How to parse javascript variable array embedded in http://up-for-grabs.net/#/?

I am trying to parse http://up-for-grabs.net/#/ to get its content in CSV file using powershell. I have written below code till now
$URL = "http://up-for-grabs.net/#/"
$HTML = Invoke-WebRequest -Uri $URL
$script_blocks = $HTML.ParsedHtml.getElementsByTagName("script") | Where{ $_.type -eq ‘text/javascript’ }
$content = ""
foreach ($script_block in $script_blocks)
{
if($script_block.innerHTML -ne $null -and `
$script_block.innerHTML.trim().StartsWith("var files"))
{
$content = $script_block.innerHTML.trim()
}
}
Looking further in the content, it seems like a variable array embedded in JavaScript whose initial lines are formatted as follows. Its array with no spaces or new lines which are my creation to improve readability.
<script type="text/javascript">
var files = {
"aspnet-razor-4":{"name":"ASP.NET Razor 4","desc":"Parser and code generator for CSHTML files used in view pages for MVC web apps.","site":"https://github.com/aspnet/Razor","tags":["Microsoft","ASP.NET","Razor","MVC"], "upforgrabs":{"name":"up-for-grabs","link":"https://github.com/aspnet/Razor/labels/up-for-grabs"}},
"fsharpdatadbpedia":{"name":"FSharp.Data.DbPedia","desc":"FSharp.Data.DbPedia - An F# type provider for DBpedia","site":"https://github.com/fsprojects/FSharp.Data.DbPedia","tags":[".NET","DbPedia","F#"],"upforgrabs":{"name":"up-for-grabs","link":"https://github.com/fsprojects/FSharp.Data.DbPedia/labels/up-for-grabs"}},
"makesharp":{"name":"Make#","desc":"Use C# scripts to automate the building process","site":"https://github.com/sapiens/MakeSharp","tags":[".Net","C#","make","build","automation","tools"],"upforgrabs":{"name":"up-for-grabs","link":"https://github.com/sapiens/MakeSharp/labels/up-for-grabs"}},
"stateprinter":{"name":"StatePrinter","desc":"Automating unittest asserts and ToString() coding.","site":"https://github.com/kbilsted/StatePrinter","tags":["TDD","Unit Testing","TDD",".NET","C#","ToString","Debugging"],"upforgrabs":{"name":"Help wanted","link":"https://github.com/kbilsted/StatePrinter/labels/Help%20wanted"}}
</script>
This immediately is followed by
var projects = new Array();
for (var fileName in files) {
projects.push(files[fileName]);
}
How can I achieve similar quick parsing in powershell without writing big code with string tokenization.
After some research, I figured out that this is a JSON content for which powershell cmdlet ConvertFrom-Json needs to be used. I do not want to copy the whole script here. Please look at this GitHub location to see how to use this cmdlet effectively. Basically, you need to remember that object returned by this cmdlet is a custom object which need to be enumerated to get various properties. Its not an array, so only foreach will work to uncover the content. A small code sample is below
$file_json = $file_string | ConvertFrom-Json
$delim = " ; "
foreach ($item in $file_json | gm)
{
$props = $file_json.$($item.Name)
if($props.MemberType) {continue}
$row = $props.name.ToString()
$row += $delim + $props.desc.ToString()
$row += $delim + $props.site.ToString()
}
Searching for this cmdlet on web will give you more details on how to deal with this conversion.

Run script in wkhtmltopdf process

I use wkhtmltopdf to create pdf(s) from html(s)
I have next function :
private void CreateTempPdf(string htmlPath, string pdfPathTemp)
{
var processorInfo = new System.Diagnostics.ProcessStartInfo
{
Arguments =
"--margin-top 27 \"" + htmlPath + "\" \"" + pdfPathTemp +
"\" ",
FileName = PublisherConfigurationManager.Pdf2HtmlConverter,
UseShellExecute = true
};
using (var proc = new System.Diagnostics.Process())
{
proc.StartInfo = processorInfo;
proc.Start();
proc.WaitForExit();
}
}
in which i pass paths of html file and destination file.
I want to add some js script to run, before pdf will be generated.
I add my code : --run-script <js> into Arguments after pdfPathTemp but script isn't applied to pdf. I also add it before --margin but this case also doesn't help me.
How correctly add scripts into wkhtmltopdf process?
I would simply add the script directly into the HTML page. In this case I would load the HTML from the path into a string, inject the script, then write a tempfile for the process duration.
As for why --run-script does not work, I have no idea. Have you tried it directly in the command line with a very simple script and HTML to see if a minimal example works for you?
If that is not an option, you might have to play around with different files for differnt js arguments, if you require such things.

YUI: Problem in file Upload and processing script..!

I have developed an interface in YUI. In which there is a form to upload a file on submit click.
YAHOO.util.Connect.setForm(document.getElementById('settings_Form'),true);
YAHOO.util.Connect.initHeader('Content-type','text/javascript');
callbackFn = {
upload: function(html) {
eval(html.responseText);
}
};
YAHOO.util.Connect.asyncRequest('POST', 'pgRequests.php?t=settings', callbackFn);
pgRequests.php processes the file uploaded and returns some javascript codes like :
var servResponse = {"global_onOff":0,"off_msg":"OFF","id":0,"service":"Change Settings"};
But when "upload" is called, the 'html' variable contains the javascript code enclosed with
<pre></pre> tags. thats why eval() function is not working there.
I tried setting header in PHP file using :
header("Content-type: text/javascript; charset: UTF-8");
header("Cache-Control: must-revalidate");
$ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() - 1) . " GMT";
header($ExpStr);
but didnt work.
I searched on google, but many people are facing the same problem. Thats why i cant figure out
where its getting wrong.
What should i do to make it work?
Is there any parallel way to implement the same flow?
Thanks in advance..!!
In your JSON encode -
replace < with <
and also
replace & with &
:p
The YUI file upload occurs through an iframe. The responseText is the body of that iframe, hence where the <pre> tags come from.
var responseNoPreTags = htmle.responseText.replace( /<\s*pre.*?>/g, '').replace ( /<\s*\/\s*pre\s*.*?>/g, '');
That should remove the <pre> tags aswell as any attributes. you should then be able to eval responseNoPreTags.

Categories

Resources