faqts : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 2 people (50%) answered Yes
Recently 1 of 2 people (50%) answered Yes

Entry

Looking for ideas on how to include file "color.txt" but only if it contains the word "green".

Mar 1st, 2009 08:52
Sarah Turner, healthy man, dman, Averick Just, http://www.ttnr.org


Ok... Got it to query and include (echo) a single file (if contents 
contained the keyword)
$file="/full/path/to_file/text2.txt"; 
$fd = fopen ($file, "r");
$contents = file($file);
$number = count($contents);
for($i=0;$i<$number;$i++) { if(strstr($contents[$i],'green')) echo 
$contents[$i]; }
Also got it to query several files and include (echo) only those that 
contained the keyword.
$file_names = array 
('text1.txt', 'text2.txt', 'text3.txt', 'text4.txt'); 
foreach($file_names as $file) {
    $contents = implode("",file("$file"));
    if(strstr($contents,'green')) { echo nl2br($contents); }
}
Naturally the above would be enclosed in PHP start/end tags. And an 
else statement can be tagged on (pardon the pun). Something like this 
should do it:
$file_names = array 
('text1.txt', 'text2.txt', 'text3.txt', 'text4.txt'); 
foreach($file_names as $file) {
    $contents = implode("",file("$file"));
    if(strstr($contents,'green')) { echo nl2br($contents); }
 else {
    include($other);
} }
Thanks go to the nice folks over at devshed.
Later,
averick
http://www.healthdothealth.com
http://www.healthxhealth.com
http://www.mesotheliomainmesothelioma.com
http://www.healthfirewall.com
http://www.flathealth.com
http://www.healthbodyguard.com
http://www.healthbyhealth.com
http://www.healthment.com
http://www.mesotheliomadown.com
http://www.mesotheliomaout.com
http://www.pickarticle.com
http://voyagevacances.blogtact.com/
http://rejseferie.blogtact.com/
http://flyvningertilschweiz.blogtact.com/
http://volssuisse.blogtact.com/
http://volsromeitalie.blogtact.com/
http://viajesvacaciones.blogtact.com/
http://reisenchina.blogtact.com/
http://flugeschweiz.blogtact.com/
http://resasemester.blogtact.com/
http://viaggivacanze.blogtact.com/
http://reisenurlaub.blogtact.com/
http://reizenvakanties.blogtact.com/
http://volisvizzera.blogtact.com/
http://gartenpflege.blogtact.com/
http://geschenkideeen.blogtact.com/
http://vluchtenromeitalie.blogtact.com/