homepage Welcome to WebmasterWorld Guest from 54.145.182.50
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Simple php function
expert help needed
Roel

10+ Year Member



 
Msg#: 3411335 posted 11:10 am on Aug 2, 2007 (gmt 0)

I need a very simple piece of php code that does the following:

INPUT: a string - let's say $a

1. open a .txt file, check if the file already contains a line that equals $a and if so echo on the screen "contains $a"
2. if not, add $a to the end of the file on a new line

Thanks!

 

dreamcatcher

WebmasterWorld Senior Member dreamcatcher us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3411335 posted 11:17 am on Aug 2, 2007 (gmt 0)

$string = '$a';

$file = file [uk2.php.net]('file.txt');

if (in_array [uk2.php.net]($string,$file))
{
// exists
}
else
{
$fp = fopen [uk3.php.net]('file.txt', 'ab');

if ($fp)
{
fwrite [uk3.php.net]($fp,$string);
fclose [uk3.php.net]($fp);
}
}

Not tested, but should give you some pointers.

dc

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved