Programming Basics
  Home arrow Programming Basics arrow Page 33 - PHP Strings Primer
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Dedicated Servers  
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PROGRAMMING BASICS

PHP Strings Primer
By: Matt Wade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2003-07-11

    Table of Contents:
  • PHP Strings Primer
  • The Basics
  • Single Quotes
  • Double Quotes
  • Heredoc
  • Concatenation
  • Displaying Strings
  • echo
  • print
  • printf
  • Strings Formatting
  • Preparing user input for comparisons
  • Capitalization
  • Reversing strings
  • Padding strings
  • Multiple Lines
  • Data Preparation
  • Adding and Removing Slashes
  • Dealing with HTML Tags and Entities
  • Counting
  • Checking password strength
  • Generating Statistics
  • Substrings (and searching)
  • Extracting Substrings
  • Counting Paragraphs
  • Filtering Words
  • Working with email addresses
  • Manually Stripping Tags
  • Password Strength Revisited
  • Handling URLs and Base64-encoding
  • Parsing URLs
  • Encoding for URLs
  • Encoding for Email
  • Hashing
  • Verifying Integrity
  • User Authentication
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    PHP Strings Primer - Encoding for Email


    (Page 33 of 37 )

    Sending email is one of the most common tasks PHP is used for. Sending binary data in those emails is not quite as common, but it is worth studying just the same. In order to send binary data through email, you must prepare it first as most email systems will destroy the data if it is not encoded. The reason for this is that email was only meant to work with a given set of characters. Any characters outside of that range will not be transported by email properly. This is where the base64_encode function comes in. It encodes data into a character set that will properly transport through email. In this section we will see how to send a simple binary attachment through email.

    But, before we jump into sending binary data with the PHP mail functions, let's first take a quick look at how to send a simple email with PHP. For sending mail, PHP provides the 'mail()' function. In order to use the mail function, we pass it a series of strings containing information such as the recipient, subject, and message. We can also specify additional headers for the email such as a reply-to address and content types. First, let's take a look at the prototype for the mail function, and then we will examine an example of how to utilize it.

    bool mail (string to, string subject, string message
              [, string additional_headers
              [,string additional_parameters]])

    As you can see, the first three parameters are mandatory. That is all that is needed for a simple email. The following example will only use the first three parameters. When we show you how to send an email with an attachment of binary data, we will utilize the additional headers.

    <?php
    $to 
    'someone@something.com';
    $subject 'Test Message';
    $message 'This is just a test message.';
    mail ($to$subject$message);
    ?>

    This is PHP in its simplest form. It will send an email with the specified subject and message to the email address in the $to variable. Now that we have seen how email is sent with PHP, let's take a look at how to send an attachment as well. As mentioned, we will need to encode the attachment so that it will properly transport through the email.

    To accomplish the task of encoding the binary data properly, we can use the 'base64_encode()' function. This function will accept the data as a string and return a string that is encoded properly. To properly format this encoded data, you should then pass it to the 'chunk_split()' function we studied earlier. Using the default parameters for 'chunk_split()' will yield a string that is broken at every 76th character, with the '\r\n' character combination inserted. Now, let's see the prototype for each of these two functions, and then we will look at an example of sending an attachment with PHP.

    string base64_encode ( string data)

    string chunk_split ( string body [, int chunklen [, string end]])

    Beyond encoding the binary data, in order to send the mail we must also specify some additional headers to the mail function. We must let it know that we will be sending MIME mail. This let's the program that sends the email know to expect the message to not be just plain text. We will also specify the content type in the headers. In the content type header we will tell the mail program how different parts of the message will be separated. This separator is just a unique string that will come before and after each part of the message.

    Then, within the body of the message we will specify what type of data each part is. Now, let's take a look at a small script that will send an email with a jpg image attachment.

    <?php
    $to 
    'someone@something.com';
    $subject 'Test Message';
    $message 'This is just a test message.';
    $jpgimage '/path/to/image.jpg';

    $border "--==================_785422457==";

    /* This places the contents of the file
       into a string and then encodes it with
       base64_encode. It also uses chunk_split
       to format the text properly */
    $fd fopen($jpgimage"r");
    $contents fread($fdfilesize($jpgimage));
    $encoded=chunk_split(base64_encode($contents));
    fclose($fd);

    $body .= "--$border\n";
    $body .= "Content-Type: text/plain; charset=us-ascii\n\n";
    $body .= $message;
    $body .= "\n--$border\n";
    $body .= "Content-Type: image/jpeg; name=\"$jpgimage\"\n";
    $body .= "Content-Transfer-Encoding: base64\n";
    $body .= "Content-Disposition: attachment; filename=\"$jpgimage\"\n";
    $body .= "\n";
    $body .= "$encoded";
    $body .= "--$border\n";

    $headers "Mime-Version: 1.0\n";
    $headers .= "Content-Type: multipart/mixed; boundary=\"$border\"\n";

    mail ($to$subject$body$headers);
    ?>

    You obviously will need to change the values in this script to reflect your personal settings.

    We will not cover the details of processing an email with PHP, but if you were to do so you would use the 'base64_encode()' to decode anything encoded with 'base64_decode()'.

    More Programming Basics Articles
    More By Matt Wade


       · comment
       · really goood work ,it covers all the major string functions.also explained with...
       · test'ng
     

    PROGRAMMING BASICS ARTICLES

    - Loops and PHP Decision Making
    - Operators, Conditionals, and PHP Decision-Ma...
    - PHP Decision-Making
    - Coding
    - Server Statistics
    - Looping in PHP
    - Cookies in PHP
    - Working with text files
    - Beginning Object Oriented Programming in PHP
    - A Tour of Decision Making Structures in PHP
    - PHP Strings Primer
    - PHP Control Structures
    - Intro to Vim
    - Reading Directorys with PHP
    - An Overview of Arrays in PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway