Jump to content

Unwanted spaces


ian2k01

Recommended Posts

Hello, I have a simple yet frustrating problem here. I'm on this project where it parses all customer information as variables then insert it into database, and aimed for paypal's transaction detail pages.

 

When parsing the customer's name, there are 4 spaces between end of name and the closing </td> tag. so this is what the source code looks like:

<tr valign="top">
<td align="right" class="label">Sent by:</td>
<td><br class="textSpacer"></td>
<td class="small">Annita Greuncuard    </td>
</tr>

 

I have tried using str_replace to get rid of the "    " with "", or preg_match_all here

preg_match_all('~(.*)    ~', $trMatches[1][0], $name);

 

however, both results come back to be negative, and the database would read the  "    " as "Â Â Â Â ". Very frustrating.

 

Any insight?

Link to comment
Share on other sites

You're making this harder than it should be. First of all, use trim before you insert data into the DB so there won't be whitespaces afterwards. But trim would take care of the spaces for you.

 

As you suggested, I have tried both trim and rtrim but it still gives me the same output, with 4 spaces at the end. Is there another way to do so?

Link to comment
Share on other sites

The spaces could be non-breaking spaces ( ), so try to convert those to regular spaces and then trim() the string:

 

$str = trim(str_replace(' ', ' ', $str));

 

This did not seem to do anything.

 

I am quite sure that they are spaces, because any of the methods above seem to work only when I copy and paste out the result onto a new file.

Link to comment
Share on other sites

If the database is reading them as "Â Â Â Â", they might not be space characters at all.  Perhaps just your text editor is displaying them as space characters.

 

 

Try looking at the file in a hex editor or something to make sure they're really spaces.

Link to comment
Share on other sites

What corbin said ;)

 

If they were simple spaces, trim() would remove them (if they are at the start or at the end of the string, that is). What does a var_dump() on the string output? And also show us the string you run through.

 

BTW, you could try to strip any whitespace (if it is whitespace at all) from the end of the string with this:

 

$str = preg_replace('~\s*$~D', '', $str);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.