Jump to content


Photo

Read lines starting with in PHP


  • Please log in to reply
1 reply to this topic

#1 vidyashankara

vidyashankara
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 15 June 2006 - 09:24 PM

TITLE PHP TEXT
HEADER PHP HEADER
!@##@
123!@#
$#%@#$
sdFSDAF
SDFSDF

Lets say we have the above text file, how i select lines starting with TITLE or HEADER? I have the following code currently

$contents = file_get_contents(file.txt);
$title=  preg_replace('/^(?!TITLE).*?(?:\n|$)/m','',$contents);

This does the job, But the script turns up a blank screen now and then because i have 5 of such lines in the script. The preg_replace command is the problem because the script runs really fast when i comment it out. Is there any better way to select lines starting with certain words?

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 18 June 2006 - 07:47 PM

This might be the long way around it, but it works...
<?php
$file = file("file.txt");
foreach($file as $line)
    if(strpos($line,"TITLE") === 0)
        echo "<p>$line</p>";
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users