| | #1 |
| Brainstorming Wizard War Room Member Join Date: 2004 Location: Illinois
Posts: 451
Thanks: 31
Thanked 174 Times in 83 Posts
|
I have a line of code that is the following: $customer_email_address = 'customer@yourdomain.com'; I want to replace customer@yourdomain.com so it is replaced by a flat text file in a folder on the same server as the script I know the following is wrong but to give you an idea of what I want to do is to include the folder and its text file: $customer_email_address = 'emailfolder/email.txt'; and the email.txt would have the email address such as customer@yourdomain.com Anybody know how to do this? Thanks, George |
| |
| | #2 |
| Engineer War Room Member Join Date: 2007 Location: In The Machine
Posts: 627
Thanks: 151
Thanked 103 Times in 72 Posts
|
Yeh, you can't simply include the file and have it parsed into a variable. What you have to do is read the file contents and put that into a variable or array. It's quite simple. 10 minute job. In fact, there is another solution. If all you want is one email address from that file, you can just name the file as the email address, and use a simple function to fetch the file by extension, then extract the name, which will be the email. It will be better as you don't have to save any files, you can simply change the name of the file and you could do it in 2 lines of php |
| >> 20 Years Full-Stack Engineer (frontend, backend, devops), Game Dev (UE5), AI Systems Dev << Available for Fixed Fee Projects and Hourly Contract Work | |
| |
| | #3 |
| VIP Warrior War Room Member Join Date: 2008 Location: Montreal Canada
Posts: 2,390
Thanks: 1,017
Thanked 836 Times in 572 Posts
Blog Entries: 1 |
Hi George I'm guessing that you intend to place a series of customer e-mail addresses within the file and you want to read them sequentially? I would start by reading the file contents into an array, then cycling through the array and assign it's values to $customer_email_address. Here is an article about reading text files into an array with PHP: PHP Tutorials: Reading a text file into an array Another good tutorial regarding using text files with PHP is at PHP Tutorial - File Read And this tutorial shows you how to read multiple values from a comma-delimited text file - this may be useful if you want to read additional parameters (such as the customer name associated with the e-mail address) : Reading a Delimited File using PHP Hope this is of some help to you, best of luck with your project. Bill |
| |
| | #4 |
| Brainstorming Wizard War Room Member Join Date: 2004 Location: Illinois
Posts: 451
Thanks: 31
Thanked 174 Times in 83 Posts
|
Thanks Damian and Bill. The link from above did the trick for me. $myFile = "testFile.txt"; $fh = fopen($myFile, 'r'); $theData = fread($fh, 5); fclose($fh); echo $theData; from PHP Tutorial - File Read Thanks again for the help guys. George |
| |
| | #5 |
| Active Warrior War Room Member Join Date: 2009
Posts: 43
Thanks: 0
Thanked 11 Times in 5 Posts
|
Just a little info.. Instead of code above George, you can use a single line, like: $customer_email_address = file_get_contents('testFile.txt'); file_get_contents is perfect for getting content from a file into a string, if that's all you need. |
| |
| | #6 | |
| Brainstorming Wizard War Room Member Join Date: 2004 Location: Illinois
Posts: 451
Thanks: 31
Thanked 174 Times in 83 Posts
|
Thanks, George | |
| |
| | #7 |
| Brainstorming Wizard War Room Member Join Date: 2004 Location: Illinois
Posts: 451
Thanks: 31
Thanked 174 Times in 83 Posts
|
LessThanWeb, The code didn't come out right in the quote last post, but I did try your original post code, FYI. George |
| |
| | #8 |
| Active Warrior War Room Member Join Date: 2009
Posts: 43
Thanks: 0
Thanked 11 Times in 5 Posts
|
Wrong path to the file since you received blank page? That's the only thing that can break it.. |
| |
| | #9 |
| Brainstorming Wizard War Room Member Join Date: 2004 Location: Illinois
Posts: 451
Thanks: 31
Thanked 174 Times in 83 Posts
| |
| |
| | #10 |
| Active Warrior War Room Member Join Date: 2009
Posts: 43
Thanks: 0
Thanked 11 Times in 5 Posts
|
Oh hehe, yes, to output it you need echo ![]() Glad it works! |
| |
| | #11 |
| Engineer War Room Member Join Date: 2007 Location: In The Machine
Posts: 627
Thanks: 151
Thanked 103 Times in 72 Posts
|
By the way, to have a more manageable format, like an array, you can use this: $customer_emails = file('testFile.txt'); Then you can access each line with: $customer_emails[0] $customer_emails[1] etc etc To view the details as the array, do this (2 lines): $customer_emails = file('testFile.txt'); print_r($customer_emails); To go through the array and print the list to screen with one email per line, do this (5 lines): $customer_emails = file('testFile.txt'); foreach($customer_emails as $customer_email){ echo $customer_email.'<br />'; } Hope that helps. I use similar functions all the time for parsing keyword txt files from google so I can paste into bulk domain name searches. Damien. |
| >> 20 Years Full-Stack Engineer (frontend, backend, devops), Game Dev (UE5), AI Systems Dev << Available for Fixed Fee Projects and Hourly Contract Work | |
| |
|
| Bookmarks |
| Tags |
| php, question, variable, varuable |
| |