Page 1 of 1
first occurence in a file??
Posted: Sat Sep 07, 2002 8:45 am
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]
Posted: Sat Sep 07, 2002 8:59 am
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>";
}
?>
How does this move the file pointer??
Posted: Sat Sep 07, 2002 9:21 am
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.
Posted: Sat Sep 07, 2002 9:26 am
by Coco
obviously not
but it does give you an offset number
Posted: Sat Sep 07, 2002 9:29 am
by gite_ashish
ok... relax...

i didn't got it correctly....
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....

Almost... ... ... closer
Posted: Sat Sep 07, 2002 10:13 am
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]
Posted: Sat Sep 07, 2002 12:05 pm
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";
}
?>
Posted: Sat Sep 07, 2002 12:30 pm
by twigletmac
Aggressiveness is not the key to quick and useful help

...
Mac
Posted: Sat Sep 07, 2002 2:25 pm
by Takuma
The key to quick and useful help is "Be nice, give full details especially error message and the code".
Agressiveness ????
Posted: Sat Sep 07, 2002 6:45 pm
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
Posted: Sat Sep 07, 2002 8:39 pm
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.
Posted: Sun Sep 08, 2002 12:48 am
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!