ted_chou12 Posted January 21, 2007 Share Posted January 21, 2007 what I see in the strpos() function is that the place of the letter gets echoed out, but is it possible, or are there are there other functions that echo out the line number of that specific string?ThanksTed Link to comment https://forums.phpfreaks.com/topic/35083-can-strpos-find-the-line-number-of-the-txt/ Share on other sites More sharing options...
trq Posted January 21, 2007 Share Posted January 21, 2007 You would need to make your own function for this. Use [url=http://php.net/file]file[/url] to pull a file into an array, then use [url=http://php.net/array_ keys]array_keys[/url] to find the line number. Link to comment https://forums.phpfreaks.com/topic/35083-can-strpos-find-the-line-number-of-the-txt/#findComment-165583 Share on other sites More sharing options...
Nameless12 Posted January 21, 2007 Share Posted January 21, 2007 $lines = explode("\n", $file_text); Link to comment https://forums.phpfreaks.com/topic/35083-can-strpos-find-the-line-number-of-the-txt/#findComment-165589 Share on other sites More sharing options...
trq Posted January 21, 2007 Share Posted January 21, 2007 [quote]$lines = explode("\n", $file_text);[/quote]Why would you do that when...[code=php:0]$lines = file($file_text);[/code]is more efficient? Link to comment https://forums.phpfreaks.com/topic/35083-can-strpos-find-the-line-number-of-the-txt/#findComment-165592 Share on other sites More sharing options...
Nameless12 Posted January 21, 2007 Share Posted January 21, 2007 I was just trying to explain to him in one line how line numbers are created Link to comment https://forums.phpfreaks.com/topic/35083-can-strpos-find-the-line-number-of-the-txt/#findComment-165598 Share on other sites More sharing options...
ted_chou12 Posted January 21, 2007 Author Share Posted January 21, 2007 thanks Nameless12,but thorpe, i understand file() but i dont understand array_keys, the example it give isnt even clear, and the result it shown is something like an array instead of numbers...Ted Link to comment https://forums.phpfreaks.com/topic/35083-can-strpos-find-the-line-number-of-the-txt/#findComment-165609 Share on other sites More sharing options...
Nameless12 Posted January 21, 2007 Share Posted January 21, 2007 [code]foreach ($file as $key => $line) {if ($line == 'some_string') { $save = $line; $line_num = $key;}}[/code]is the above any help? Link to comment https://forums.phpfreaks.com/topic/35083-can-strpos-find-the-line-number-of-the-txt/#findComment-165617 Share on other sites More sharing options...
trq Posted January 21, 2007 Share Posted January 21, 2007 [code]<?php $lines = file($textfile); echo "'this line' appears on line number ".array_keys($lines,'this line')+1;?>[/code] Link to comment https://forums.phpfreaks.com/topic/35083-can-strpos-find-the-line-number-of-the-txt/#findComment-165619 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.