I have problems cleaning up a couple of input field. I've been pecking away at this issue since I wrote the application about 18 months ago. When I think I have it cleaned up; it pops up again, when users violate rules (as they often will do) and the extra spaces and tabs show up.

I have a form that is filled via Copy from an excel file (this is a requirement) and pasted into the textarea:

<textarea name="header_fields" cols="60" rows="6"><?php if(!empty($error)) {echo $varAllText;} ?></textarea>

When the form is processed, the field is received via the following:

$varAllText = trim($_POST['header_fields']);
        list($Supplier_Name, $Booking_Number, $Container_Number, $BOL_Number, $Factory_Departure_Date, $Sailing_Date, $ETA_date ) = $varList = explode("\r\n", $varAllText);

I created a function to clean off the occasional extra spaces, and tabs (the tabs happen when someone violates the excel template and copies one or two extra fileds - the delimeter is the tab)

function stringCleaner($string){
    $string2 = preg_replace("/\s+/", "", $string);
    return $string2;

Then I call it for all three of the critial fields:

    $Booking_Number = stringCleaner($Booking_Number);
    $Container_Number = stringCleaner($Container_Number);
    $BOL_Number = stringCleaner($BOL_Number);

And then just because I'm worried about it, I do one last trim:

$Container_Number = trim($Container_Number);

And yet, I still find that on occasion, I still have the spaces and the tab at the end of the entry

So the bill of lading looks like [MRP14041081 ] instead of [MRP14041081] and the Container Number looks like [ABCD5323970 ] instead of [ABCD5323970]; so when I feed that into the Query to retreive the record, it cannot be found because of the extra characters.

How do I clean up the mess in the string. I've tried (on my own) to trim it, to use regex, and as you see I have not been able to figure out a way to reliably clean up the problem.

Any and all suggestions are greatly appreciated.


3 Years
Discussion Span
Last Post by diafol

You say that copy/paste is a requirement, but it may be easier to save as csv and upload that file. Or is this not allowed?

Anyway the tab encoding is '\t', if you want to use that in your regex. Trim could be:


However, trim() should get rid of all whitespace by default.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.