vidyashankara Posted June 15, 2006 Share Posted June 15, 2006 [code] TITLE PHP TEXTHEADER PHP HEADER!@##@123!@#$#%@#$sdFSDAFSDFSDF[/code]Lets say we have the above text file, how i select lines starting with TITLE or HEADER? I have the following code currently[code]$contents = file_get_contents(file.txt);$title= preg_replace('/^(?!TITLE).*?(?:\n|$)/m','',$contents);[/code]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? Link to comment https://forums.phpfreaks.com/topic/12107-read-lines-starting-with-in-php/ Share on other sites More sharing options...
zq29 Posted June 18, 2006 Share Posted June 18, 2006 This might be the long way around it, but it works...[code]<?php$file = file("file.txt");foreach($file as $line) if(strpos($line,"TITLE") === 0) echo "<p>$line</p>";?>[/code] Link to comment https://forums.phpfreaks.com/topic/12107-read-lines-starting-with-in-php/#findComment-47077 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.