Jump to content

Archived

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

Reeksy

Finding a point within a text file

Recommended Posts

Hi

I have a line of code within a text file. Is it possible to search the file for that line of code and then return it?

Jon

Share this post


Link to post
Share on other sites
If you want a specific line number

[code]
<?php
  $file = file('data.txt');
  // to get line 28
  echo $file[27];
?>
[/code]

Share this post


Link to post
Share on other sites
OK thanks.

Problem is i don't always know the line number! However i do know the text string.

Is it possible to search the file for an instance of that text string?

Share this post


Link to post
Share on other sites
How big is the file, or how big may the file become that you want to search? You can read line by line for big files and slurp for small files. So the size of your file will determine the best way to search within it!


me!

Share this post


Link to post
Share on other sites
The file would never be bigger than 100K. Usually around 50K.

Does that help!?

Share this post


Link to post
Share on other sites
so, you're simply searching for the occurence of a pre-defined string within a text file?
[code]
<?php
$text = file_get_contents('myFile.txt');
$search = "My Text";
if (strpos($text, $search)) {
  // search string is in file
} else {
  // not in the file
}
?>
[/code]

Share this post


Link to post
Share on other sites
can you please tell me for what it will be use??

i mean i which situation you are using this please tell me

Share this post


Link to post
Share on other sites
[quote author=Barand link=topic=110671.msg447948#msg447948 date=1160137474]
But what if the file begins with "My Text"?
[/quote]
i suppose that would call for a preg_match() or other type search at that point:
[code]
<?php
$text = file_get_contents('myFile.txt');
$search = "|My Text|i";
if (preg_match($search, $text) {
  // search string is in file
} else {
  // not in the file
}
?>
[/code]

Share this post


Link to post
Share on other sites
[quote author=rajmohan link=topic=110671.msg447951#msg447951 date=1160137784]
can you please tell me for what it will be use??

i mean i which situation you are using this please tell me

[/quote]

OK the scenario is: I have a link on my website, this link fires off to a map on an external website. However this external map service requires the coordinates of the item you’re looking at. The coordinates are specific to an item and can change so ideally they need to be dynamically pulled out.

My problem is I don’t have the coordinates to pull out dynamically. Therefore I need to copy the exact link from another website which does have the up-to-date coordinates.

So I need to get a line of code from their source code. I can get the contents using file_get_contents() I just need to know how to find the line where that link is and return it. I wont always know the line number but I do know the name of the link (which only appears once within the source code)

Hope that makes sense!

Share this post


Link to post
Share on other sites
so, you're wanting to pull the line number of the matching text:
[code]
<?php
$handle = fopen($file, 'r');
$lMatch = -1; // default to a recognizable failure value
$search = "|my text|i";
if ($handle) {
  $num = 0;
  while (!feof($handle)) {
    $line = fgets($handle);
    $num++; // increment line number
    if (preg_match($search, $line)) {
      $lMatch = $num;
      break;
    }
  }
  fclose($handle);
}

if ($lMatch < 0) {
  // no matches
} else {
  // match found:
  echo "Line $lMatch contains the search string";
}
?>
[/code]

now you just have to figure out if they want all matching line numbers if there are more than one. that would take some slight modification to the code above.

Share this post


Link to post
Share on other sites
Job Done!

Ok thanks a lot guys. Much appreciated.

I'm sure I'll be back soon!

Jon

Share this post


Link to post
Share on other sites

×

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.