Jump to content

Search after read a file


Neopyros

Recommended Posts

Hi, i'm new and already with a problem  ;D

 

 

Here's the problem, i've read a file, in this file, i've the same word n times, i need to return the last position of the word and the text below.

 

Ex:

File -

Test

Test

Name

Test

Test

Name

Test

Name

Ok

 

Search for Name

Should return Last position of the word Name and Ok

 

I can't make it return the position of the words found, any ideas?

 

<?php
    $open = @fopen("test.txt", "r");
    $text = fread($open,filesize("test.txt "));
    fclose($open);
    $vetor = explode("\n", $texto);
    print("\n<pre>");
    print_r($vetor);
    print("\n\n\n");
    $size = sizeof($vetor);
    for($i=0;$i<$size;$i++){
if($vetor[$i] == 'Name'){
	print(strpos($vetor[$i]));
}
    }
         
?>

 

 

Link to comment
Share on other sites

i would reverse the array first (since you're searching for the last instance of 'Name') and use the key as an indicator of which line it resides on (remembering to add 1, since the first key is 0):

 

<?php
    $open = @fopen("test.txt", "r");
    $text = fread($open,filesize("test.txt "));
    fclose($open);
    $vetor = explode("\n", $text);
    print("\n<pre>");
    print_r($vetor);
    print("\n\n\n");

    $reverse = array_reverse($vetor, TRUE);
    foreach($reverse AS $line => $val)
    {
      if ($val == 'Name')
      {
        echo 'Last instance of "Name" occurs on line '.($line+1);
        break;
      }
    }
?>

 

if you want to keep everything on that line and after, simply use a for() starting at $i = $line and ending at the size of the array.

Link to comment
Share on other sites

<?php

$file = "test.txt";
$search = "Name";

/****************************/

$lines = file($file);
$positions = array();

foreach($lines as $lineno => $line) {
if(trim($line) == $search) {
  $positions[] = $lineno;
}
}

sort($positions);
$start = end($positions);

for($i = $start; $i <= (count($lines) - 1); $i++) {
echo "{$lines[$i]}<br />\n";
}

?>

 

Completely untested, let's hope it works!

Link to comment
Share on other sites

Darkwinter, a paste it wrong in the php file is all text

 

i would reverse the array first (since you're searching for the last instance of 'Name') and use the key as an indicator of which line it resides on (remembering to add 1, since the first key is 0):

if you want to keep everything on that line and after, simply use a for() starting at $i = $line and ending at the size of the array.

 

I already try that one, but returns nothig... Just like mine =(

 

Chigley, thanks a lot, it works!

 

Thanks guys for the help

 

 

Link to comment
Share on other sites

if you're not receiving any output, then the problem resides in your file opening, because it means you haven't actually read from the file.  remove the @ error suppression, make sure error reporting is turned on, and run it again.  take a look at the errors you receive.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.