Jump to content

Need help getting the position of a string inside a file (in bytes)


bicho83

Recommended Posts

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.