bicho83 Posted April 11, 2008 Share Posted April 11, 2008 If I want to search for a string inside a .txt file, lets say for example that the .txt has the following information "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like)." Lets say I want to search for the string "The point of using Lorem Ipsum", and (what I really want to know) is to get the position of where that string starts at, in bytes (this is the only option as far as I know). This is what I got: $text_to_find = '/The point of using Lorem Ipsum/'; $filename = file_get_contents("raw_data.txt"); $handle = fopen("raw_data.txt","r"); if (preg_match($text_to_find,$filename)) { echo "Text String Found"; $position = ftell($handle, preg_match($text_to_find,$filename)); //where are we (in bytes)? echo $position; } else { echo "Text String NOT Found"; } When I run that script it displays "Text String Found", and "Warning: Wrong parameter count for ftell() in line 6". Ftell is expecting bytes (or numbers) in the second parameter, but I don't know how to tell him that dynamically, because that file changes all the time. Link to comment https://forums.phpfreaks.com/topic/100578-need-help-getting-the-position-of-a-string-inside-a-file-in-bytes/ Share on other sites More sharing options...
doni49 Posted April 11, 2008 Share Posted April 11, 2008 ftell takes ONE argument--the file pointer. Link to comment https://forums.phpfreaks.com/topic/100578-need-help-getting-the-position-of-a-string-inside-a-file-in-bytes/#findComment-514407 Share on other sites More sharing options...
bicho83 Posted April 11, 2008 Author Share Posted April 11, 2008 you are right, I tried with fgets and it shows "Text String Found" with nothing else. How do I get the position in bytes then? Link to comment https://forums.phpfreaks.com/topic/100578-need-help-getting-the-position-of-a-string-inside-a-file-in-bytes/#findComment-514410 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.