|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
|||
|
|||
|
Using str_replace() to read a text file and evaluate?
I am trying to write a script that will work the following way:
User uses a form to add a user to the site. The script writes the value ($username) to a text file like this: $username| $username2| $username3| I need a script that would prevent duplicate $username values to be written to the text file. Should I be using str_replace? I am kind of new to PHP, but I learn pretty quickly ![]() Please write back. Any help would be greatly appreciated ![]() |
|
#2
|
|||
|
|||
|
If the usernames are on a line to themselves then scan through each line at a time comparing it to the new username. If you reach the end of the file then the username is not present. If they are delimited in some way then read the whole thing into a string and use the split function to chop it up. Then you can compare each entry in the array to the new username.
-KM- |
|
#3
|
|||
|
|||
|
KM,
Thanks for the response. Is there anyway that you could give me an example of how to do what you are stating here? The text file which is storing the names has each username on a different line with the "|" character after it. Thanks!!! ![]() -mochahete |
|
#4
|
|||
|
|||
|
I have since been trying to figure out what KM meant. I have this:
function addtomainteachlist($teacher) { $TheFile = "mainteacherlist.txt"; $fp = fopen("mainteacherlist.txt", 'a+'); chmod("mainteacherlist.txt", 0777); $implode_me = array($teacher, "\n"); $noduplics = array_unique($implode_me); $string = implode('|', $noduplics); fwrite($fp, $string); fclose($fp); print ("Data Collected!<BR>\n"); } |
|
#5
|
|||
|
|||
|
If they are stored one per line don't bother with the | since the newline will act as a delimiter. To check through the file open it using fopen in mode 'r' (read) and go through it one line at a time and compare.
Have a go at that and post your code up, -KM- |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > Using str_replace() to read a text file and evaluate? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|