This is a discussion on Problem wwith strcmp() function within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Dear group. Can someone please tell me why the following program is not finding a match? Is something wrong in ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Dear group.
Can someone please tell me why the following program is not finding a match? Is something wrong in the strcmp() function ? When I leave out the strcmp() and only take the while loop with the print command inside, it prints all the 10 lines from the file tekst. ========================= <?php $filename=tekst; $targetline="line 5"; $fd = fopen($filename, "r"); while(!feof($fd)) { $line = fgets($fd,80); if(strcmp($line, $targetline) == 0) { echo "A match was found!"; print("$line <br>"); } } fclose($fd); ?> ========================= File tekst: ======= line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 line 9 line 10 ======= Rgds, Henk Oegema |
|
|||
|
Henk Oegema wrote:
> Dear group. > > Can someone please tell me why the following program is not finding a match? > Is something wrong in the strcmp() function ? > When I leave out the strcmp() and only take the while loop with the print > command inside, it prints all the 10 lines from the file tekst. > > ========================= > <?php > > $filename=tekst; > $targetline="line 5"; > > > $fd = fopen($filename, "r"); > > while(!feof($fd)) > { > $line = fgets($fd,80); > if(strcmp($line, $targetline) == 0) > { > echo "A match was found!"; > print("$line <br>"); > } > } > > fclose($fd); > > ?> > ========================= > > File tekst: > ======= > line 1 > line 2 > line 3 > line 4 > line 5 > line 6 > line 7 > line 8 > line 9 > line 10 > ======= > > Rgds, > Henk Oegema > Caveat: I have not tested this I think the problem you are having is that fgets returns the entire line including the carriage-return. So, 'line 5' does not equal 'line 5\r\n'. Try doing the following: if( strncmp($line, $targetline, strlen($targetline)) { and see if that helps. -david- |
|
|||
|
David Haynes wrote:
> Henk Oegema wrote: >> Dear group. >> >> Can someone please tell me why the following program is not finding a >> match? Is something wrong in the strcmp() function ? >> When I leave out the strcmp() and only take the while loop with the print >> command inside, it prints all the 10 lines from the file tekst. >> >> ========================= >> <?php >> >> $filename=tekst; >> $targetline="line 5"; >> >> >> $fd = fopen($filename, "r"); >> >> while(!feof($fd)) >> { >> $line = fgets($fd,80); >> if(strcmp($line, $targetline) == 0) >> { >> echo "A match was found!"; >> print("$line <br>"); >> } >> } >> >> fclose($fd); >> >> ?> >> ========================= >> >> File tekst: >> ======= >> line 1 >> line 2 >> line 3 >> line 4 >> line 5 >> line 6 >> line 7 >> line 8 >> line 9 >> line 10 >> ======= >> >> Rgds, >> Henk Oegema >> > > Caveat: I have not tested this > > I think the problem you are having is that fgets returns the entire line > including the carriage-return. So, 'line 5' does not equal 'line 5\r\n'. > Try doing the following: > > if( strncmp($line, $targetline, strlen($targetline)) { > > and see if that helps. > > -david- After a lot of trying I have not been able to get it working yet. I changed $targetline="line 5" to $targetline="line 5\r\n"; I also tried to change if(strcmp($line, $targetline) == 0) to if(strcmp($line, $targetline, strenlen($targetline)) == 0) Henk |
|
|||
|
Henk Oegema wrote:
> David Haynes wrote: > >> Henk Oegema wrote: >>> Dear group. >>> >>> Can someone please tell me why the following program is not finding a >>> match? Is something wrong in the strcmp() function ? >>> When I leave out the strcmp() and only take the while loop with the print >>> command inside, it prints all the 10 lines from the file tekst. >>> >>> ========================= >>> <?php >>> >>> $filename=tekst; >>> $targetline="line 5"; >>> >>> >>> $fd = fopen($filename, "r"); >>> >>> while(!feof($fd)) >>> { >>> $line = fgets($fd,80); >>> if(strcmp($line, $targetline) == 0) >>> { >>> echo "A match was found!"; >>> print("$line <br>"); >>> } >>> } >>> >>> fclose($fd); >>> >>> ?> >>> ========================= >>> >>> File tekst: >>> ======= >>> line 1 >>> line 2 >>> line 3 >>> line 4 >>> line 5 >>> line 6 >>> line 7 >>> line 8 >>> line 9 >>> line 10 >>> ======= >>> >>> Rgds, >>> Henk Oegema >>> >> Caveat: I have not tested this >> >> I think the problem you are having is that fgets returns the entire line >> including the carriage-return. So, 'line 5' does not equal 'line 5\r\n'. >> Try doing the following: >> >> if( strncmp($line, $targetline, strlen($targetline)) { >> >> and see if that helps. >> >> -david- > > After a lot of trying I have not been able to get it working yet. > > I changed $targetline="line 5" to $targetline="line 5\r\n"; > I also tried to change > if(strcmp($line, $targetline) == 0) > to > if(strcmp($line, $targetline, strenlen($targetline)) == 0) > > Henk > Henk: This code works on my system: <?php $filename='tekst'; $targetline='line 5'; $fd = fopen($filename, 'r'); while(!feof($fd)) { $line = fgets($fd, 80); //echo "line = $line"; if(strncmp($line, $targetline, strlen($targetline)) == 0) { echo "A match was found! "; print("$line <br>"); } } fclose($fd); ?> Also, the line ending will be either '\n' for Linux/Unix or '\r\n' for Windows. That is why using strncmp is a better choice. -david- |
|
|||
|
David Haynes wrote:
> Henk Oegema wrote: >> David Haynes wrote: >> >>> Henk Oegema wrote: >>>> Dear group. >>>> >>>> Can someone please tell me why the following program is not finding a >>>> match? Is something wrong in the strcmp() function ? >>>> When I leave out the strcmp() and only take the while loop with the >>>> command inside, it prints all the 10 lines from the file tekst. >>>> >>>> ========================= >>>> <?php >>>> >>>> $filename=tekst; >>>> $targetline="line 5"; >>>> >>>> >>>> $fd = fopen($filename, "r"); >>>> >>>> while(!feof($fd)) >>>> { >>>> $line = fgets($fd,80); >>>> if(strcmp($line, $targetline) == 0) >>>> { >>>> echo "A match was found!"; >>>> print("$line <br>"); >>>> } >>>> } >>>> >>>> fclose($fd); >>>> >>>> ?> >>>> ========================= >>>> >>>> File tekst: >>>> ======= >>>> line 1 >>>> line 2 >>>> line 3 >>>> line 4 >>>> line 5 >>>> line 6 >>>> line 7 >>>> line 8 >>>> line 9 >>>> line 10 >>>> ======= >>>> >>>> Rgds, >>>> Henk Oegema >>>> >>> Caveat: I have not tested this >>> >>> I think the problem you are having is that fgets returns the entire line >>> including the carriage-return. So, 'line 5' does not equal 'line 5\r\n'. >>> Try doing the following: >>> >>> if( strncmp($line, $targetline, strlen($targetline)) { >>> >>> and see if that helps. >>> >>> -david- >> >> After a lot of trying I have not been able to get it working yet. >> >> I changed $targetline="line 5" to $targetline="line 5\r\n"; >> I also tried to change >> if(strcmp($line, $targetline) == 0) >> to >> if(strcmp($line, $targetline, strenlen($targetline)) == 0) >> >> Henk >> > > Henk: > This code works on my system: > <?php > $filename='tekst'; > $targetline='line 5'; > > $fd = fopen($filename, 'r'); > while(!feof($fd)) { > $line = fgets($fd, 80); > //echo "line = $line"; > if(strncmp($line, $targetline, strlen($targetline)) == 0) { > echo "A match was found! "; > print("$line <br>"); > } > } > fclose($fd); > ?> > > Also, the line ending will be either '\n' for Linux/Unix or '\r\n' for > Windows. That is why using strncmp is a better choice. > > -david- YES David, when I changed $targetline="line 5\r\n" to $targetline="line 5\n" it worked for me too. Thanks very much. |