Forum Moderators: coopster
this is sentence one
this is sentence two
this is sentence three
Now lets say I need to read that file and see if the word "three" exists anywhere within the file. I have been reading it into an array and searching for the string "this is sentence three" but I am getting a return of false. I have tried arr_search, in_array, and foreach searching each sub of the array.
Basically what I need to do is see if it's in there because I am writing to the file.
Let's say for instance "this is sentence four" is not in the text file, then I want to write "this is sentence four" to the file. However, if it is already written to the file then we don't want to write it. Any ideas on how to implement this. I can paste my code, but it is rather complicated because I think I'm going off on the wrong tangent. This can't be that hard. Thanks,
$string = 'this is sentence two';
$file = file_get_contents('some_file.txt');
if([url=http://www.php.net/strpos]strpos[/url]($file, $string) !== FALSE)
echo 'The file contains the string';
else
echo 'The string is not within the file.';
You could also do a case-insensitive search using stripos [php.net].
//this function writes the download hyperaccess file it takes in an
//array of file paths i.e. $path must be glob("some path"); then
//it searches these files based on my criteria to grab a user id
//if that user id download command is already in the script it does
//not write it. otherwise we add that command
function writeDownload($path){
//the name of the file to be sent
$sendingFileName = substr($file, 29);
$fp = @fopen((trim($file)), 'r');
$arrayExplode = explode("*", fread($fp, filesize($file)));
//get all of the names to write to the file
foreach($path as $i){
$fp = @fopen((trim($i)), 'r');
$arrayExplode = explode("*", fread($fp, filesize($i)));
//if it is the 2b guy then we grab 7 characters otherwise we grab 8 for the user id
if($arrayExplode[43] == "S21762B~PER"){
array_push($userName,(substr($arrayExplode[43], 0, 7)));
}
else{
array_push($userName, (substr($arrayExplode[43], 0, 8)));
}//else
}//foreach
$fname = ("\\\\comm04\\My Files\\ANSI - MS BCBS DOWNLOAD.txt");
$file = fopen($fname, "w");
//set my date
$date = date("m/d/y");
$date .=" at ";
$date .=date("h:i:s");
//write the initial commands for the script
if(fwrite($file, "\$LANG = \"VBScript\"\n".
"' VBS script learned for \"M - TRANSMIT - ANSI - MS BCBS.HAW on $date.\n\n".
"cr = Chr(13)\n\n".
"haAbortOnError\n\n".
"haWaitForPrompt \"gon Id: \"\n".
"haTypeText \"S2176\"&cr\n\n".
"haWaitForPrompt \"ssword: \"\n".
"haTypeText \"ECLAIM\"&cr\"\n\n"))
fclose($fname);
else{
echo("There was an error in renaming the file");
die();
}
fclose($fname);
//now we add to the file everytime there is a user id that needs to download
foreach($userName as $i){
$fname = ("\\\\comm04\\My Files\\ANSI - MS BCBS DOWNLOAD.txt");
$file = fopen($fname, "a+");
//store the contents of the file into a string
$lines = file_get_contents($fname);
//if the line has already been written.
if(strpos($lines,"haTypeText \"\$\$REQUEST ID=$i BID='HCFAREPORT'\"&cr") !== FALSE){
}//if strpos
else{
//the command has not been written so we add it.
if(fwrite($file, "haWaitForPrompt cr&Chr(10)&cr&\"cmd> \"\nhaTypeText \"\$\$REQUEST ID=$i BID='HCFAREPORT'\"&cr\n\n"));
else{
echo"There was an error in writing the download batch file.";
die();
}//else
}//else
fclose($fname);
}//foreach
//write the logoff command
$fname = "\\\\comm04\\My Files\\ANSI - MS BCBS DOWNLOAD.txt";
$file = fopen($fname, "a");
if(fwrite($file,"haWaitForPrompt cr&Chr(10)&cr&\"cmd> \"\nhaTypeText \"\$\$LOGOFF\"&cr\n\nhaTerminate"));
else{
echo("There was an error in writing the download file.");
die();
}//else
}//writeDownload