Jump to content


Photo

Finding a point within a text file


  • Please log in to reply
11 replies to this topic

#1 Reeksy

Reeksy
  • New Members
  • Pip
  • Newbie
  • 8 posts
  • LocationSomerset, UK

Posted 06 October 2006 - 08:45 AM

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

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 06 October 2006 - 09:02 AM

If you want a specific line number

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 Reeksy

Reeksy
  • New Members
  • Pip
  • Newbie
  • 8 posts
  • LocationSomerset, UK

Posted 06 October 2006 - 11:05 AM

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?

#4 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 06 October 2006 - 11:32 AM

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!

#5 Reeksy

Reeksy
  • New Members
  • Pip
  • Newbie
  • 8 posts
  • LocationSomerset, UK

Posted 06 October 2006 - 11:42 AM

The file would never be bigger than 100K. Usually around 50K.

Does that help!?

#6 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 06 October 2006 - 12:20 PM

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

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 06 October 2006 - 12:24 PM

But what if the file begins with "My Text"?
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#8 rajmohan

rajmohan
  • Members
  • PipPipPip
  • Advanced Member
  • 169 posts

Posted 06 October 2006 - 12:29 PM

can you please tell me for what it will be use??

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


#9 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 06 October 2006 - 12:43 PM

But what if the file begins with "My Text"?

i suppose that would call for a preg_match() or other type search at that point:
<?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
}
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#10 Reeksy

Reeksy
  • New Members
  • Pip
  • Newbie
  • 8 posts
  • LocationSomerset, UK

Posted 06 October 2006 - 01:07 PM

can you please tell me for what it will be use??

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


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!


#11 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 06 October 2006 - 01:15 PM

so, you're wanting to pull the line number of the matching text:
<?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";
}
?>

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.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#12 Reeksy

Reeksy
  • New Members
  • Pip
  • Newbie
  • 8 posts
  • LocationSomerset, UK

Posted 06 October 2006 - 01:24 PM

Job Done!

Ok thanks a lot guys. Much appreciated.

I'm sure I'll be back soon!

Jon




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users