SitePoint Sponsor

User Tag List

Results 1 to 14 of 14

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Putting in line breaks

    Gudday all
    I have a long string called $message. Currently it places all the text on a single line.
    PHP Code:
    <?php
    ...
    $message "I would a copy of the Petals & Patches catalogue send to me please - 
       Media: 
    $media
       Name: 
    $name 
       Address: 
    $address 
       City/Town: 
    $city
       State: 
    $state
       Post code: 
    $postcode
       Country: 
    $country
       E-mail: 
    $email"
    ....
    ?>
    I would like to have the form of
    I would a copy of the Petals & Patches catalogue send to me please -
    Media: $media
    Name: $name
    Address: $address
    City/Town: $city
    State: $state
    ..
    What would I need to do to the PHP code to achieve this?
    ========================
    Carn the Tiges!
    www.petalsandpatches.com

  2. #2
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heredoc syntax.

    PHP Code:
    $message = <<<TEXT
    I would a copy of the Petals & Patches catalogue send to me please - 
    Media: 
    $media
    Name: 
    $name 
    Address: 
    $address 
    City/Town: 
    $city
    State: 
    $state
    TEXT; 

  3. #3
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Shouldn't make a difference, line breaks in double quoted strings get through as well.

    I would guess that whatever is outputting it is expecting HTML, so you need to put <br> at the end of each line.

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry should have been more explicit.
    An e-mail is being generated.
    When those ideas still work?
    ========================
    Carn the Tiges!
    www.petalsandpatches.com

  5. #5
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stormrider View Post
    Shouldn't make a difference, line breaks in double quoted strings get through as well.
    D'oh. Of course they do .
    Last edited by decowski; May 15, 2009 at 08:32.

  6. #6
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Do emails require \r\n rather than just \n? If so, convert plain newlines into carriage returns with newlines.

    PHP Code:
    $message str_replace("\n""\r\n"$message); 
    Salathe
    Software Developer and PHP Manual Author.

  7. #7
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes, SMTP standard requires \r\n I think. Most mail servers will accept either, but the strict ones only \r\n.

    You don't want to replace \n with \r\n without then making sure you replace \r\r\n with \r\n :P

  8. #8
    SitePoint Zealot
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Gents
    Thanks for your help
    I have tried the code you noted but are getting a parse error
    "Parse error: syntax error, unexpected $end in C:\wamp\www\PetalsandPatches\cataloguetest-3.php on line 34"

    Line 34 is outside the code.

    I reckon the problem is the if loop but after reading the doco I am no wiser. Unless I can make use of the \r\n?

    PHP Code:
    <?
    ...
    if (
    $name != "" AND $address != "" AND $city != "" AND $state != "" AND $postcode != ""
     {
      
    $sendto "tlknell@somewhere.com.au"
      
    $subject "Petals & Patches catalogue request"
      
    $message = <<<TEXT
      I would a copy of the Petals & Patches catalogue send to me please - 
      Media: 
    $media
      Name: 
    $name 
      Address: 
    $address 
      City/Town: 
    $city
      State: 
    $state
      Post code: 
    $postcode
      Country: 
    $country
      E-mail: 
    $email"; 
      TEXT; 
      // sending e-mail
      mail("
    $sendto", "$subject", "$message");
      //echo "Your message was sent";
      header('Location: thankyou.htm');
      exit();
     }
    ?>
    Any advice?
    ========================
    Carn the Tiges!
    www.petalsandpatches.com

  9. #9
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    The line containing TEXT; needs to have no whitespace before it. The only thing that the line should contain is TEXT; - nothing more, nothing less.
    Salathe
    Software Developer and PHP Manual Author.

  10. #10
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That error means you have an unclosed block of code (in other words a missing bracket).

  11. #11
    SitePoint Zealot
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Gents
    Here is the full PHP code
    PHP Code:
    <?php
    $media 
    $_POST['media'];
    $name $_POST['name'];
    $address $_POST['address'];
    $city $_POST['city'];
    $state $_POST['state'];
    $postcode $_POST['postcode'];
    $country $_POST['country'];
    $email $_POST['email'];

    $sendto "tlknell@tpg.com.au"
    $subject "Petals & Patches catalogue request"
    $message = <<<TEXT
    I would a copy of the Petals & Patches catalogue send to me please - 
    Media: 
    $media
    Name: 
    $name 
    Address: 
    $address 
    City/Town: 
    $city
    State: 
    $state
    Post code: 
    $postcode
    Country: 
    $country
    E-mail: 
    $email"; 
    TEXT; 
    // sending e-mail
    mail("
    $sendto", "$subject", "$message");
    header('Location: thankyou.htm');
    exit();
    ?>
    I have removed the if loop to eliminate any sort of error from that angle.
    I am still getting a parse error.
    ========================
    Carn the Tiges!
    www.petalsandpatches.com

  12. #12
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As Salathe writes, the TEXT; line should have no whitespace before or after (except newline) and it seems you have a single space character after TEXT;.

    The code does work after removing the space, as you can see.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •