[nycphp-talk] Reading just a couple lines of a file?
Tom Melendez
tom at supertom.com
Wed Mar 26 13:26:48 EDT 2008
Only going to do this once? Any chance that you'll want to
extract/manipulate data from these files later on? Would it make
sense to batch load them into a DB and work with them via SQL somehow
so you could work with them later? Or process in the background?
Thanks,
Tom
http://www.liphp.org
On Wed, Mar 26, 2008 at 9:28 AM, Donald J Organ IV
<dorgan at donaldorgan.com> wrote:
>
> if its valid xhtml you could always use SimpleXML, and just get the title.
> http://www.php.net/SimpleXML
>
>
> csnyder wrote:
> On 3/26/08, Ben Sgro <ben at projectskyline.com> wrote:
>
>
> Hello,
>
> Performance: Yeah, you don't have to hold the entire file in memory. Use
> fopen/fread and iterate through
> with a buffer size.
>
>
> - Ben
>
> And since you're searching for something specific, you can even just
> go line by line using fgets() till you find what you want:
>
> $fp = fopen( $file, r );
> while( !feof($fp) ) {
> $line = fgets( $fp );
> if ( strpos( $line, '<title' ) !== FALSE ) {
> break;
> }
> }
> if ( !feof( $fp ) ) {
> // get title out of $line here
> }
>
> Since you're working with big files, it makes sense to build a better
> limit into the while loop, to bail out if the title isn't found after
> 99 lines or something.
>
>
>
>
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
>
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php
>
More information about the talk
mailing list