Register Advertise with usHelp Desk Today's Posts Search

Closed Thread
Thread Tools Search this Thread
Unread 1st Sep 2009, 12:47 PM   #1
Brainstorming Wizard
War Room Member
 
George Sepich's Avatar
 
Join Date: 2004
Location: Illinois
Posts: 451
Thanks: 31
Thanked 174 Times in 83 Posts
Default
PHP Variable Question
Share on: 
fb share twitter share gplus share more share

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
George Sepich is offline  
Unread 1st Sep 2009, 01:11 PM   #2
Engineer
War Room Member
 
Damien Roche's Avatar
 
Join Date: 2007
Location: In The Machine
Posts: 627
Thanks: 151
Thanked 103 Times in 72 Posts
Default
Re: PHP Variable Question
Share on: 
fb share twitter share gplus share more share

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
Damien Roche is offline  
Unread 1st Sep 2009, 01:30 PM   #3
VIP Warrior
War Room Member
 
ereadergold's Avatar
 
Join Date: 2008
Location: Montreal Canada
Posts: 2,390
Thanks: 1,017
Thanked 836 Times in 572 Posts
Blog Entries: 1
Default
Re: PHP Variable Question
Share on: 
fb share twitter share gplus share more share

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
ereadergold is offline  
Unread 1st Sep 2009, 01:44 PM   #4
Brainstorming Wizard
War Room Member
 
George Sepich's Avatar
 
Join Date: 2004
Location: Illinois
Posts: 451
Thanks: 31
Thanked 174 Times in 83 Posts
Default
Re: PHP Variable Question
Share on: 
fb share twitter share gplus share more share

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
George Sepich is offline  
Unread 1st Sep 2009, 02:23 PM   #5
Active Warrior
War Room Member
 
Join Date: 2009
Posts: 43
Thanks: 0
Thanked 11 Times in 5 Posts
Default
Re: PHP Variable Question
Share on: 
fb share twitter share gplus share more share

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.
LessThanWeb is offline  
Unread 1st Sep 2009, 03:41 PM   #6
Brainstorming Wizard
War Room Member
 
George Sepich's Avatar
 
Join Date: 2004
Location: Illinois
Posts: 451
Thanks: 31
Thanked 174 Times in 83 Posts
Default
Re: PHP Variable Question
Share on: 
fb share twitter share gplus share more share

Originally Posted by LessThanWeb View Post

Just a little info..

Instead of code above George, you can use a single line, like:

= 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.
I did give the above a try, but for some reason it didn't pull the text content, and just gave me a blank screen.

Thanks,
George
George Sepich is offline  
Unread 1st Sep 2009, 03:44 PM   #7
Brainstorming Wizard
War Room Member
 
George Sepich's Avatar
 
Join Date: 2004
Location: Illinois
Posts: 451
Thanks: 31
Thanked 174 Times in 83 Posts
Default
Re: PHP Variable Question
Share on: 
fb share twitter share gplus share more share

LessThanWeb,
The code didn't come out right in the quote last post, but I did try your original post code, FYI.

George
George Sepich is offline  
Unread 2nd Sep 2009, 06:17 AM   #8
Active Warrior
War Room Member
 
Join Date: 2009
Posts: 43
Thanks: 0
Thanked 11 Times in 5 Posts
Default
Re: PHP Variable Question
Share on: 
fb share twitter share gplus share more share

Wrong path to the file since you received blank page?

That's the only thing that can break it..
LessThanWeb is offline  
Unread 2nd Sep 2009, 02:07 PM   #9
Brainstorming Wizard
War Room Member
 
George Sepich's Avatar
 
Join Date: 2004
Location: Illinois
Posts: 451
Thanks: 31
Thanked 174 Times in 83 Posts
Default
Re: PHP Variable Question
Share on: 
fb share twitter share gplus share more share

Originally Posted by LessThanWeb View Post

Wrong path to the file since you received blank page?

That's the only thing that can break it..
I added

Echo $customer_email_address; and then it showed up.

$customer_email_address = file_get_contents('testFile.txt');
Echo $customer_email_address;

Thanks,
George
George Sepich is offline  
Unread 3rd Sep 2009, 01:33 AM   #10
Active Warrior
War Room Member
 
Join Date: 2009
Posts: 43
Thanks: 0
Thanked 11 Times in 5 Posts
Default
Re: PHP Variable Question
Share on: 
fb share twitter share gplus share more share

Oh hehe, yes, to output it you need echo
Glad it works!
LessThanWeb is offline  
Unread 3rd Sep 2009, 09:22 AM   #11
Engineer
War Room Member
 
Damien Roche's Avatar
 
Join Date: 2007
Location: In The Machine
Posts: 627
Thanks: 151
Thanked 103 Times in 72 Posts
Default
Re: PHP Variable Question
Share on: 
fb share twitter share gplus share more share

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
Damien Roche is offline  
Closed Thread


Bookmarks

Tags
php, question, variable, varuable


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 05:23 PM.