Neopyros Posted July 19, 2007 Share Posted July 19, 2007 Hi, i'm new and already with a problem 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 https://forums.phpfreaks.com/topic/60792-search-after-read-a-file/ Share on other sites More sharing options...
thedarkwinter Posted July 19, 2007 Share Posted July 19, 2007 Hi strripos(http://uk2.php.net/manual/en/function.strripos.php) return the last position of of a string also you have two different variable names: $text, and $texto; $text = fread($open,filesize("test.txt ")); fclose($open); $vetor = explode("\n", $texto); Link to comment https://forums.phpfreaks.com/topic/60792-search-after-read-a-file/#findComment-302443 Share on other sites More sharing options...
akitchin Posted July 19, 2007 Share Posted July 19, 2007 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 https://forums.phpfreaks.com/topic/60792-search-after-read-a-file/#findComment-302448 Share on other sites More sharing options...
chigley Posted July 19, 2007 Share Posted July 19, 2007 <?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 https://forums.phpfreaks.com/topic/60792-search-after-read-a-file/#findComment-302451 Share on other sites More sharing options...
Neopyros Posted July 19, 2007 Author Share Posted July 19, 2007 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 https://forums.phpfreaks.com/topic/60792-search-after-read-a-file/#findComment-302465 Share on other sites More sharing options...
chigley Posted July 19, 2007 Share Posted July 19, 2007 So did I read the instructions right? I thought I misunderstood! Link to comment https://forums.phpfreaks.com/topic/60792-search-after-read-a-file/#findComment-302479 Share on other sites More sharing options...
akitchin Posted July 19, 2007 Share Posted July 19, 2007 let's have a rehash of the problem - what code are you CURRENTLY using, and what output are you receiving? Link to comment https://forums.phpfreaks.com/topic/60792-search-after-read-a-file/#findComment-302486 Share on other sites More sharing options...
Neopyros Posted July 19, 2007 Author Share Posted July 19, 2007 let's have a rehash of the problem - what code are you CURRENTLY using, and what output are you receiving? I'm using now the one i posted, the output is nothing Link to comment https://forums.phpfreaks.com/topic/60792-search-after-read-a-file/#findComment-302560 Share on other sites More sharing options...
akitchin Posted July 19, 2007 Share Posted July 19, 2007 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 https://forums.phpfreaks.com/topic/60792-search-after-read-a-file/#findComment-302562 Share on other sites More sharing options...
chigley Posted July 19, 2007 Share Posted July 19, 2007 What's wrong with mine? :'( Link to comment https://forums.phpfreaks.com/topic/60792-search-after-read-a-file/#findComment-302567 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.