[PHP] Re: Fwrite() vs file_put_contents()

2009-03-03 Thread RottenEye

I Clancy,

Have you tried to convert your content to an .ini file? I heard that the 
php function parse_ini_file is very fast for reading this kind of 
files, although for writing you have to use some sort of php class, that 
can be slower than your method.

Depends what you trying to do I guess.

Regards


Clancy wrote:

I have been pondering whether it would be feasible to work with a 100,000 entry 
index
file, and had put yesterday aside to do some timing tests. I first generated 
some sample
index files of various lengths. Each entry consisted of a single line with the 
form

ASDF;rhubarb, rhubarb, 

where the ASDF is a randomly generated four character index, and the rest of 
the line is
filling, which varies slightly in length and contents from line to line, just 
in case
something tried to get smart and cache the line.  The average lenth of the line 
is about
80 bytes.

Then I wrote another program which read the file into an array, using the four 
character
index as the key, and the filling as the contents, sorted the array, and then 
rewrote it
to another file, reporting the elapsed time after each step.

My first version used fgets() to read the source file a line at a time, and 
fwrite() to
write the new file. This version performed quite consistently, and took 
approximately 1.3
seconds to read in a 100,000 entry 7.86Mb file, and another 5 seconds to write 
it out
again.

I then read the discussion following fschnittke's post File write operation 
slows to a
crawl ...  and wondered if the suggestions made there would help.

First I used file() to read the entire file into memory, then processed each 
line into the
form required to set up my matrix. This gave a useful improvement for small 
files, halving
the time required to read and process a 10,000 entry 815 kB file, but for a 
30,000 entry
file it had dropped to about 15%, and it made little difference for a 300,000 
entry file.

Then I tried writing my whole array into a single horrendous string, and using
file_put_contents() to write out the whole string in one bang. I started 
testing on a
short file, and thought I was onto a good thing, as it halved the time to write 
out a
10,000 entry 800 K file. But as I increased the file size it began to fail 
dismally. With
a 30,000 entry file it was 20% slower, and at 100,000 entries it was three 
times slower.

On Shawn McKenzie's suggestion, I also tried replacing fgets() with 
stream_get_line(). As
I had anticipated any difference was well within below the timing noise level.

In conclusion, for short (1MB!) files, using file() to read the whole file into 
memory is
substantially better than using fgets() to read the file a line at a time, but 
the
advantage rapidly diminishes for longer files. Similarly  using 
file_put_contents() in
place of fwrite() to write it out again is better for short files (up to 
perhaps 1 MB) but
the performance deteriorates rapidly above this.



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Which file Included me?

2009-02-22 Thread RottenEye

But we could use rawurlencode($_SERVER['PHP_SELF']), no?

João


Michael A. Peters wrote:

Nisse Engström wrote:

On Wed, 18 Feb 2009 10:37:53 -0800, Michael A. Peters wrote:

http://www.gfx-depot.com/forum/-php-server-php-self-validation-t-1636.html 



explains a technique to validate the input as well (don't trust that 
is clean)


Amazing! Not once did they mention htmlspecialchars().


/Nisse



htmlspecialchars causes problems if you are going to use the data with 
DOMDocument.


I believe the point was to produce a proper _SERVER['PHP_SELF'] - not a 
sanitized but still borked version.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: DOMDocument help

2009-02-12 Thread RottenEye
?php
$string = 'this bis/b a string bthat/b has some bold 
bwords/b';

$doc = new DOMDocument;
$doc-loadHTML($string);

$items = $doc-getElementsByTagName('b');

for( $i = 0; $i  $items-length; $i++ ){
echo $items-item($i)-nodeValue . br /\n;
}
?


Michael A. Peters mpet...@mac.com wrote in message 
news:4993f090.9090...@mac.com...
 I'm using php 5.2.5 with the php-xml module.

 I need to split a string of html into elements.

 IE -

 this bis/b a string bthat/b has some bold bwords/b in it.

 I need to be able to take that string and split it up into textNodes for 
 the parts that are not in bold text, and bold nodes containing the bold 
 elements.

 I've tried creating a new DOMDocument object and inputing the line into 
 it -

 $tmpNode = new DOMDocument;
 $tmpNode-loadHTML($string);

 but I can't figure out how to get the the useful nodes out of it to then 
 append as children in the document I'm creating.

 Any tips on how to do this? 



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php