first occurence in a file??

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
Vmusic
Forum Newbie
Posts: 10
Joined: Sat Sep 07, 2002 8:45 am
Location: Columbus, Ohio

first occurence in a file??

Post by Vmusic »

Hi,
I am trying to find a php function that will let me find the first occurence or a character (or chacter with a set) in a file and move a file pointer to it. For example, move the file pointer to the first 'a-z' or 'A-Z'.

Please, Please, Please I can NOT use some linux or unix script, I can ONLY use php.

I am opening an MS Word document and tyring to move the file pointer past the junk at the top or begining to where the first REAL characters start.

I tried fscan() - but it doesn't seem to work right?

Any ideas or help would be appreciated!!!!

[/b]
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Post by gite_ashish »

hi,

try this:

Code: Select all

<?php

$fp = fopen( "file.doc", "r+" );

$contents = fread( $fp, filesize("file.doc"));

eregi( "їa-z]", $contents, $result );

for ( $i = 0; $i < count($result); $i++ )
{
    echo "$i) " . $resultї$i] . "<BR>";
}

?>
Vmusic
Forum Newbie
Posts: 10
Joined: Sat Sep 07, 2002 8:45 am
Location: Columbus, Ohio

How does this move the file pointer??

Post by Vmusic »

So please explain.. how does dumping the contents of a file into a string and doing a pattern match on the string (not the file) move the file pointer to the first occurence a character or character set in a file ???????


It really doesn't.
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

obviously not

but it does give you an offset number
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Post by gite_ashish »

ok... relax... ;-)
i didn't got it correctly.... :oops:

Code: Select all

<?php
$fp = fopen( "file.doc", "r+" );

$contents = fread( $fp, filesize("file.doc")); 

// can't use regular expression here
$loc = strpos( $contents, "searchString" );

fseek( $fp, $loc );

?>

hope this time i m doing well.... 8)
Vmusic
Forum Newbie
Posts: 10
Joined: Sat Sep 07, 2002 8:45 am
Location: Columbus, Ohio

Almost... ... ... closer

Post by Vmusic »

HOWEVER,
strpos();

Can this REALLY locate a 'set of characters' for example, find the position of ANY of the following characters [a-z] or [A-Z] or does strpos() ONLY look for the starting position of one 'string' in another......


???

Thanks For Trying,
(Close Only Counts In Horse Shoes)[/u][/b]
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

For that you'll probably have to use Regular Expression. Fore more details see http://www.phpbuilder.com/columns/dario19990616.php3. I don't know any function that can get the starting pos of string. So you'd probably use both ereg and strlen.
Example:

Code: Select all

<?php
$string = "hello how are you";
$tmp = "";

if(ereg("how",$string)) {
  $tmp = ereg_replace("how","21761938746317286318369",$string);
  list($first,$second) = split("21761938746317286318369",$tmp);
  $length = strlen($first);
  $length++;

  echo "starting position is $length";
}
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Aggressiveness is not the key to quick and useful help :roll: ...

Mac
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

The key to quick and useful help is "Be nice, give full details especially error message and the code".
Vmusic
Forum Newbie
Posts: 10
Joined: Sat Sep 07, 2002 8:45 am
Location: Columbus, Ohio

Agressiveness ????

Post by Vmusic »

Agressive?
I don't think anyone was agressive. Although I realize that many postings are somewhat vague, it is more amazing how often a reply doesn't even really address the posting.
If I reply to a posting where I don't believe there an direct answer, I always start off my posting with something like:
I don't believe there is a direct way to.... you might try .....


But I guess.... free advise (even if it isn't what you need) is some advice
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

That is because most of the time, the questions for help are not in detail enough or are not explained properly, so the person who is nice enough to help gets mixed up and explains the wrong thing. It happens, and i highly doubt it's the fault of the person who replies.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Oromian wrote:That is because most of the time, the questions for help are not in detail enough or are not explained properly, so the person who is nice enough to help gets mixed up and explains the wrong thing. It happens, and i highly doubt it's the fault of the person who replies.
Exactly!
Post Reply