Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Stream Functions : stream_get_line

stream_get_line

Gets line from stream resource up to a given delimiter (PHP 5)
string stream_get_line ( resource handle, int length [, string ending] )


Code Examples / Notes » stream_get_line

dante

My testing has found this function to be dramatically faster than fgets on PHP 5.1.14.  The difference is probably due to how buffering is used internally.  Compare the following:
<?php
// reads 10,000 lines in 27 seconds
while (!feof($handle)) {
   $line = fgets($handle, 1000000);
}
?>
vs.
<?php
// reads 10,000 lines in 0.5 seconds
while (!feof($handle)) {
   $line = stream_get_line($handle, 1000000, "\n");
}
?>


18-apr-2006 10:07

In version 5.0.4 using this funtion and then calling ftell($stream) would give you the position up to but not including the "ending" string.
When I rev'd to PHP version 5.1.2, calling this function then using ftell($stream) would give the position up to AND including the "ending" string
for example, parsing HTTP responses.
The response from apache using curl....
------------------------------------------------------------
HTTP/1.1 200 OK
Date: Tue, 18 Apr 2006 20:54:59 GMT
Server: Apache/1.3.33 (Unix) PHP/5.0.4 mod_ssl/2.8.22 OpenSSL/0.9.7e
X-Powered-By: PHP/5.0.4
Transfer-Encoding: chunked
Content-Type: text/html
<html><body>test</body></html>
-------------------------------------------------------------
The code:
<?php
 $headers = stream_get_line($in,4096,"\r\n\r\n");
  fseek ($in,ftell($in)+4);
  while (!feof($in)){
     fputs ($out,stream_get_line($in,4096,''));
  }
?>
prior to my 5.0.4 this worked perfectly, trimming the \r\n\r\n section of the HTTP response and seperating the top into the $headers string, and the rest was placed into the file handle $out.
using php 5.1.2, the above code chopps off the first 4 bytes of the HTTP response and puts
l><body>test</body></html>
into $out.


Change Language


Follow Navioo On Twitter
stream_bucket_append
stream_bucket_make_writeable
stream_bucket_new
stream_bucket_prepend
stream_context_create
stream_context_get_default
stream_context_get_options
stream_context_set_option
stream_context_set_params
stream_copy_to_stream
stream_encoding
stream_filter_append
stream_filter_prepend
stream_filter_register
stream_filter_remove
stream_get_contents
stream_get_filters
stream_get_line
stream_get_meta_data
stream_get_transports
stream_get_wrappers
stream_register_wrapper
stream_resolve_include_path
stream_select
stream_set_blocking
stream_set_timeout
stream_set_write_buffer
stream_socket_accept
stream_socket_client
stream_socket_enable_crypto
stream_socket_get_name
stream_socket_pair
stream_socket_recvfrom
stream_socket_sendto
stream_socket_server
stream_socket_shutdown
stream_wrapper_register
stream_wrapper_restore
stream_wrapper_unregister
eXTReMe Tracker