Strip End of Line Characters PHP

5 replies
Hi Guys

I have a php script that takes input from a form and emails the results. Everything works fine, but users often use the [Return] key at the end of lines and I would like to strip those characters from the variable.

What I usually get is something like this (2 paragraphs) ...

The quick brown fox
jumped over the lazy dog
The quick brown fox
jumped over the lazy dog
The quick brown fox
jumped over the lazy dog

The quick brown fox
jumped over the lazy dog
The quick brown fox
jumped over the lazy dog
The quick brown fox
jumped over the lazy dog

But what I want is this (still 2 paragraphs, but with end-of line characters stripped and paragraphs still intact) ...

The quick brown fox jumped over the lazy dog The quick brown fox jumped over the lazy dog The quick brown fox jumped over the lazy dog

The quick brown fox jumped over the lazy dog The quick brown fox jumped over the lazy dog The quick brown fox jumped over the lazy dog

Can anyone help?

Many thanks

Will
#characters #end #line #php #strip
  • Profile picture of the author cmaclean
    Hi Will,

    The trim() function should work for this. Example:

    $text = "The quick brown fox jumped over the";
    $emailThisOutput = trim($text, " \t.");


    \t represents a carriage return.

    Hope that helps.

    Craig
    {{ DiscussionBoard.errors[1473546].message }}
    • Profile picture of the author chaos69
      Originally Posted by cmaclean View Post


      t represents a carriage return.
      \t is a tab, not carriage return.
      Signature
      Best Ways To Make Money Online

      Eight bytes walk into a bar. The bartender asks, “Can I get you anything?”
      “Yeah,” reply the bytes. “Make us a double.”
      {{ DiscussionBoard.errors[1473606].message }}
  • Profile picture of the author Mr. Enthusiastic
    Will, I've done this before in other programming languages. The outline is:
    1. Search for all pairs of adjacent returns and replace them with an unusual code such as \|\.
    2. Search for all returns and delete thems.
    3. Search for the unique code and replace it with a pair of returns.

    Chris
    {{ DiscussionBoard.errors[1473583].message }}
  • Profile picture of the author Will Edwards
    Thanks guys

    Finally managed it!

    Will
    {{ DiscussionBoard.errors[1473706].message }}
    • Profile picture of the author Mr. Enthusiastic
      Originally Posted by Will Edwards View Post

      Thanks guys

      Finally managed it!

      Will
      Care to post what you came up with?
      {{ DiscussionBoard.errors[1475243].message }}

Trending Topics