![]() |
PHP Variable Question 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 |
Re: PHP Variable Question 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 ;) |
Re: PHP Variable Question 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 |
Re: PHP Variable Question 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 |
Re: PHP Variable Question 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. |
Re: PHP Variable Question Quote:
Thanks, George |
Re: PHP Variable Question LessThanWeb, The code didn't come out right in the quote last post, but I did try your original post code, FYI. George |
Re: PHP Variable Question Wrong path to the file since you received blank page? That's the only thing that can break it.. |
Re: PHP Variable Question Quote:
Echo $customer_email_address; and then it showed up. $customer_email_address = file_get_contents('testFile.txt'); Echo $customer_email_address; Thanks, George |
Re: PHP Variable Question Oh hehe, yes, to output it you need echo :) Glad it works! |
Re: PHP Variable Question 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. |
| All times are GMT -6. The time now is 05:27 PM. |