move file pointer to specofic location

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

move file pointer to specofic location

Post by yaron »

Hello all,
I need to process a small part out of a big file.
I thought first to transform the file into an array using file()
after that I need to look for a specific string (it's a substring actually) in that file array that will indicate the location which the processing should begin.
in_array() is not helpful because as I mentoined it is a substring and in_array will return false and won't give the index as well.
maybe there is another way by reading it directly from the file but then I need to move the pointer top the specified location.
any ideas that can help me?

Thanks
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

If you have enough memory to put the whole file in a var use a full string instead of an array..

Code: Select all

<?php
 $needle = 'my stuff';
 $haystack = join('',file('filename'));
 $startpos = strpos( $haystack, $needle ); // use stripos() for case insensitive
 if ($startpos === false)
 {
     // It was not found
 }
 else
 {
    // it was found at $startpos
 }
?>
Post Reply