This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-10-18 20:49:12

funelephant
Scratcher
Registered: 2010-07-02
Posts: 1000+

PHP Help!

So, I'm using fwrite, but I want the text to go on a certain line without overwriting text.

How do I do this?


nicki begs to differ
http://24.media.tumblr.com/ab0e6e8fd347c5e39c2821bcab9d16e6/tumblr_mgu35sui1L1rfb7aqo2_500.gif

Offline

 

#2 2012-10-18 21:06:18

jvvg
Scratcher
Registered: 2008-03-26
Posts: 1000+

Re: PHP Help!

You can process the text using something else and write it to the file when it's done processing.


http://tiny.cc/zwgbewhttp://tiny.cc/e1gbewhttp://tiny.cc/zygbewhttp://tiny.cc/izgbew
Goodbye, Scratch 1.4  sad                                                        Hello Scratch 2.0!  smile

Offline

 

#3 2012-10-18 21:11:52

funelephant
Scratcher
Registered: 2010-07-02
Posts: 1000+

Re: PHP Help!

jvvg wrote:

You can process the text using something else and write it to the file when it's done processing.

Uh whut.


nicki begs to differ
http://24.media.tumblr.com/ab0e6e8fd347c5e39c2821bcab9d16e6/tumblr_mgu35sui1L1rfb7aqo2_500.gif

Offline

 

#4 2012-10-18 21:19:37

jvvg
Scratcher
Registered: 2008-03-26
Posts: 1000+

Re: PHP Help!

funelephant wrote:

jvvg wrote:

You can process the text using something else and write it to the file when it's done processing.

Uh whut.

By that, I mean read the file, do something with the text independent of the file I/O functions, and then put the result in the file.


http://tiny.cc/zwgbewhttp://tiny.cc/e1gbewhttp://tiny.cc/zygbewhttp://tiny.cc/izgbew
Goodbye, Scratch 1.4  sad                                                        Hello Scratch 2.0!  smile

Offline

 

#5 2012-10-18 21:31:38

SJRCS_011
Scratcher
Registered: 2011-02-07
Posts: 1000+

Re: PHP Help!

May I ask why you're using fwrite in the first place?
If you're trying to store data, then databases are much easier and much more secure than files, with some exceptions


http://i.imgur.com/vQqtH.png
Learning to Program in a Nutshell:  "You're missing a closing parentheses" - LS97

Offline

 

#6 2012-10-18 21:33:41

jvvg
Scratcher
Registered: 2008-03-26
Posts: 1000+

Re: PHP Help!

SJRCS_011 wrote:

May I ask why you're using fwrite in the first place?
If you're trying to store data, then databases are much easier and much more secure than files, with some exceptions

However, I think funelephant is new to PHP, and files are a lot easier to use to get started (my first PHP projects, and even most of Mod Share 3 used files).

Last edited by jvvg (2012-10-18 21:34:08)


http://tiny.cc/zwgbewhttp://tiny.cc/e1gbewhttp://tiny.cc/zygbewhttp://tiny.cc/izgbew
Goodbye, Scratch 1.4  sad                                                        Hello Scratch 2.0!  smile

Offline

 

#7 2012-10-18 22:39:56

funelephant
Scratcher
Registered: 2010-07-02
Posts: 1000+

Re: PHP Help!

jvvg wrote:

SJRCS_011 wrote:

May I ask why you're using fwrite in the first place?
If you're trying to store data, then databases are much easier and much more secure than files, with some exceptions

However, I think funelephant is new to PHP, and files are a lot easier to use to get started (my first PHP projects, and even most of Mod Share 3 used files).

Yes. Yes I am new  tongue


nicki begs to differ
http://24.media.tumblr.com/ab0e6e8fd347c5e39c2821bcab9d16e6/tumblr_mgu35sui1L1rfb7aqo2_500.gif

Offline

 

#8 2012-10-19 00:12:40

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: PHP Help!

SJRCS_011 wrote:

May I ask why you're using fwrite in the first place?
If you're trying to store data, then databases are much easier and much more secure than files, with some exceptions

Actually, if you store the files in a place that isn't accessible through normal means (HTTP/web server), say, the directory above public_html (think is the folder name), it's just about as secure as databases, though not encrypted (unless you write a program to do that). I think SQL-Lite uses local files instead of a full blown database server. If you use files (instead of SQL), you can avoid SQL-Injections. Though you have to make sure your code doesn't have any ways to have code injected into it.

Offline

 

#9 2012-10-19 02:25:39

fg123
Scratcher
Registered: 2008-11-13
Posts: 1000+

Re: PHP Help!

So what the guys above me are trying to suggest, is that you use fopen and process the lines of the files into an array. Then using standard php string functions, you can do your processing. Finally, just write the array back into the file.


Hai.

Offline

 

#10 2012-10-20 06:22:41

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: PHP Help!

Are you trying to insert text into a line of an existing multiline text string? You could try using this code.

Code:

function addline($inputString, $line, $insertString){ //a function that inserts the string $insertString on a new line below the specified line number $line, in the string $InputString and returns the result. If the line number given is greater than the number of lines in the input string $inputString, it will add it at the bottom.
    $max = strlen($inputString); //make sure that the search system doesn't keep searching further than the actual length of the input
    $n = 0; //keeps track of the line count
    $c = 0; //keeps track of the character count
    for($i=0;$i<$max;$i++){ //loop through every character in your input string
        $c++;//increment the character counter by 1
        if($string[$i]=='\n'){
            $n++; // if the character equals a newline, change $n by 1.
        }
        if($n == $line){
            break; // if the line number reached is equal to the line you're looking for, leave the counting for loop.
        }
    }
    return substr($inputString, 0, $c) . $insertString . '/n' . substr($inputString, $c + 1, $max); // join together the section of the input string before the line you want to insert at, the string to input followed by a newline and then the other half of the input string and return this.
}

I hope this is vaguely understandable, I've commented the code (though I've not tried it out).

Further reading:
substr()
strlen()
for loops
functions in PHP


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#11 2012-10-20 07:45:11

funelephant
Scratcher
Registered: 2010-07-02
Posts: 1000+

Re: PHP Help!

sparks wrote:

Are you trying to insert text into a line of an existing multiline text string? You could try using this code.

Code:

function addline($inputString, $line, $insertString){ //a function that inserts the string $insertString on a new line below the specified line number $line, in the string $InputString and returns the result. If the line number given is greater than the number of lines in the input string $inputString, it will add it at the bottom.
    $max = strlen($inputString); //make sure that the search system doesn't keep searching further than the actual length of the input
    $n = 0; //keeps track of the line count
    $c = 0; //keeps track of the character count
    for($i=0;$i<$max;$i++){ //loop through every character in your input string
        $c++;//increment the character counter by 1
        if($string[$i]=='\n'){
            $n++; // if the character equals a newline, change $n by 1.
        }
        if($n == $line){
            break; // if the line number reached is equal to the line you're looking for, leave the counting for loop.
        }
    }
    return substr($inputString, 0, $c) . $insertString . '/n' . substr($inputString, $c + 1, $max); // join together the section of the input string before the line you want to insert at, the string to input followed by a newline and then the other half of the input string and return this.
}

I hope this is vaguely understandable, I've commented the code (though I've not tried it out).

Further reading:
substr()
strlen()
for loops
functions in PHP

Thanks! I'll fix it later today  big_smile

And Happy Birthday!


nicki begs to differ
http://24.media.tumblr.com/ab0e6e8fd347c5e39c2821bcab9d16e6/tumblr_mgu35sui1L1rfb7aqo2_500.gif

Offline

 

#12 2012-10-20 10:33:26

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: PHP Help!

Thanks  big_smile


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#13 2012-10-20 14:00:37

GP1
Scratcher
Registered: 2009-07-06
Posts: 1000+

Re: PHP Help!

Well, you could read each line into an array and then edit the array element to the line you want changed. Then just loop through the array, writing each line to the text file.


I am currently http://blocks.scratchr.org/API.php?user=GP1&amp;action=onlineStatus&amp;type=imagehttp://blocks.scratchr.org/API.php?user=GP1&amp;action=onlineStatus&amp;type=text and I finally got over 1000 posts.

Offline

 

Board footer