How to remove a blank line in a form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tarja311
Forum Commoner
Posts: 73
Joined: Fri Oct 20, 2006 10:57 pm

How to remove a blank line in a form

Post by tarja311 »

Hello all,

I hope i am posting in the right place. I have built a search engine and it has a basic form in place where users type in some text and it is sent and stored on the database for later retrieval via search.

Now my problem lies in the submission of the text. Usually the users copy / paste their text into the form and that sometimes leaves a bunch of blank spaces in between the lines of text. Example :

this is some text...

this is some more text...

As you can clearly see, there is a blank line in between the two lines of text. This also happens if the user hits enter before they copy /paste, and it leaves an empty line above the text.

Is there a way i can remove this before it is sent off to the database? because when this stuff is searched, the blank lines are stored as well. There are not supposed to be any empty lines like this.

I am running Windows XP. MySQL and PHP 5

Thanks

-- tarja
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

trim() function is probably worth looking at on php.net
tarja311
Forum Commoner
Posts: 73
Joined: Fri Oct 20, 2006 10:57 pm

Post by tarja311 »

Hi,

Actually i've been trying to use trim for the last three hours or so. I've looked up the examples on php.net and read the comments below that, i've even googled it, but so far i have not found an example as to how you can remove a blank space that does not contain any characters off the html form.

EDIT :

Nevermind, i've solved it. Instead of fighting with it on the PHP side, i had the database check for it instead.

Code: Select all

if($file == ""){ $sql = "DELETE FROM ....
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

Ok, good. Trim should remove whitespaces though..
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

akimm wrote:Ok, good. Trim should remove whitespaces though..
Please read the function description
trim -- Strip whitespace (or other characters) from the beginning and end of a string
What you want to do is a str_replace() for newline characters.
Post Reply