PHP Variable Question

10 replies
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
#php #question #variable #varuable
  • Profile picture of the author Damien Roche
    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
    Signature
    >> Seasoned Web Developer (CSS, JavaScript, PHP, Ruby) <<
    Available for Fixed Fee Projects and Hourly ($40/hr)
    {{ DiscussionBoard.errors[1140509].message }}
  • Profile picture of the author mywebwork
    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
    {{ DiscussionBoard.errors[1140572].message }}
  • Profile picture of the author George Sepich
    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
    Signature

    Need Help? GeorgeSepich.com Digital Marketing Solutions From George Sepich.

    {{ DiscussionBoard.errors[1140620].message }}
  • Profile picture of the author LessThanWeb
    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.
    {{ DiscussionBoard.errors[1140788].message }}
    • Profile picture of the author George Sepich
      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
      Signature

      Need Help? GeorgeSepich.com Digital Marketing Solutions From George Sepich.

      {{ DiscussionBoard.errors[1141056].message }}
  • Profile picture of the author George Sepich
    LessThanWeb,
    The code didn't come out right in the quote last post, but I did try your original post code, FYI.

    George
    Signature

    Need Help? GeorgeSepich.com Digital Marketing Solutions From George Sepich.

    {{ DiscussionBoard.errors[1141066].message }}
  • Profile picture of the author LessThanWeb
    Wrong path to the file since you received blank page?

    That's the only thing that can break it..
    {{ DiscussionBoard.errors[1142955].message }}
    • Profile picture of the author George Sepich
      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
      Signature

      Need Help? GeorgeSepich.com Digital Marketing Solutions From George Sepich.

      {{ DiscussionBoard.errors[1144385].message }}
  • Profile picture of the author LessThanWeb
    Oh hehe, yes, to output it you need echo
    Glad it works!
    {{ DiscussionBoard.errors[1146180].message }}
  • Profile picture of the author Damien Roche
    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.
    Signature
    >> Seasoned Web Developer (CSS, JavaScript, PHP, Ruby) <<
    Available for Fixed Fee Projects and Hourly ($40/hr)
    {{ DiscussionBoard.errors[1147180].message }}

Trending Topics