Jump to content

Finding a point within a text file


Reeksy

Recommended Posts

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]
Link to comment
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]
Link to comment
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!
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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