Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output Single File in Text File

Status
Not open for further replies.

mevasquez

Programmer
Aug 26, 2003
75
US
I am having some difficulty getting a single line from a text file. I can do that using grep in UNIX but having problems in php. I tried preg_grep and getting nothing. Here is what I have so far.
the php file is called getText.asp and is called from another webpage sending the value to search for, for example, getText.asp?id=10774

Code:
$id = $_GET['id'];
$path = "/path/to/file";
$filename = "nameoffile";

if (isset($id)){
  $lines = file($path . $filename);
  foreach($lines as $line){
    $item = preg_grep($id, $line);
    if($item != ""){
      echo $item;
      exit;
    }
  }
}

What am I doing wrong?

TIA
Mike
 
@ things spring immediate, why is a PHP file called gettext.[red]asp[/red](that would be an ASP file),

and second have you tried using the file() function from PHP. it will by itself convert the file contents into an array. each index in the array will contain a line from the file.

file()

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
The file is a php file. That was a typo. I have been doing some asp for awhile and now I am re-learning php. That is what my code is doing. It is getting the contents of file() and going through each line using preg_grep to find the line that contains a specific string. Right now it is getting nothing.

Mike
 
using preg_grep to find the line that contains a specific string. Right now it is getting nothing.

preg_grep is finding nothing? What does $_GET['id'] look like? The search parameter for any preg_* function family member is delimited by slashes or @-signs.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Code:
$lines = file($path .[red]'/'.[/red] $filename);

and $id is unlikely to be a regex. if you are looking for an id like 20 your regex would be
Code:
'/20/'

also you are supplying the preg_grep with a string when it takes an array as its second argument. remember aswell that the function will require an array element to be precisely 20 (say) with no other text etc on that line. is this what you intend? if you're looking for something else you will need to make your regex a little fuzzier. if you are looking to see whether 20 is in any of the array elements, then strpos may be a better bet than preg_grep.

for preg_grep
Code:
if (isset($_GET['id'])){
 $id = $_GET['id'];
 $path = "/path/to/file";
 $filename = "nameoffile";

 $lines = file($path .'/'. $filename);
 if ($lines !==false){
  $items = preg_grep($id, $lines);
  echo "<pre>".print_r($items, true);  
 }
}
 
$id is 10774

When I do the following it works
Code:
 if ($lines != false){
  $items = preg_grep('/10774/', $lines);
  echo "<pre>".print_r($items, true);  
 }

But this does not.
Code:
 if ($lines != false){
  $items = preg_grep('/$id/', $lines);
  echo "<pre>".print_r($items, true);  
 }
Not sure how to fix the syntax.

Mike
 
variables are not expanded in single quotes.
Code:
 if ($lines != false){
  $items = preg_grep([red]"/$id/"[/red], $lines);
  echo "<pre>".print_r($items, true);  
 }
 
Ok that works; however, it is outputted out $item[69]. Is there a function to get the element number of the output.

Mike
 
Never mind, I figured it out using implode.

Thanks for your help.

Mike
 
yes.
Code:
foreach ($items as $item){
 echo $lines[$item] .'<br/>';
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top