Jump to content

Removing all kind of whitespaces


Mcod

Recommended Posts

Hi there,

 

I am having issues with whitespaces, as I can't get rid of them for some reason. I tried the basics like ltrim or str_replace("  ",""); and so on, but somehow not all whitespaces are removed, no matter what I try. I know there are various things that can cause spacing, so I guess in my case it's not really spaces that are causing the issues, so maybe you have some snippet somewhere which deals with this issue. I'm basically trying to remove every possible whitespaces within a string that are more than one space - even spaces that are caused by something like TAB or similar things - I am sure I am not the first one who ran into this problem, so I am sure someone has a solution for this :)

 

Your help is greatly appreciated as usual.

Link to comment
Share on other sites

Hi Mcod,

 

Try this code, it should do what you want.

 

Input:

two  spaces. Space+tab here. Three  spaces_and_tab.

 

Code:

<?php 
$pattern='~\s{2,}~';
$replacement=' ';
$subject='two  spaces. Space+tab 	here. Three   	spaces_and_tab.';
$s=preg_replace($pattern,$replacement,$subject);
echo $s.'<br />';
?>

 

Output:

two spaces. Space+tab here. Three spaces_and_tab.

 

Note that the replacement string is one space. You can make it an empty string if you like.

If this is not quite what you want, please supply an example.

 

:)

Link to comment
Share on other sites

Hi there,

 

I think it must be something else, not a tab or a space. When I copied the whitespaces, they just became regular spaces, but replacing spaces didn't work. So I tried to do a utf8_decode on the string and it di throw out wat you can see in the attached image. This happens if I set the header to utf-8 and use utf8_decode.

 

When I use iso-8859-1 as header without utf8_decode, then I get some strange A character with some accent on it and a space.

 

Maybe this little debug info will help you to help me :)

post-127171-13482403164679_thumb.png

Link to comment
Share on other sites

Hi Mcod,

 

Here's a trick I learned a few days ago on another forum to debug a string with "hidden characters".

I would try it on some of your input.

 

 

Input:

Hello Mcod

 

Code:

<?php 
$string = 'Hello 	Mcod';
for($i=0;$i<strlen($string);$i++) echo "{{$string[$i]}}" . ord($string[$i]) . " <br />\n";
?>

 

Output:

{H}72

{e}101

{l}108

{l}108

{o}111

{ }32

{ }9

{M}77

{c}99

{o}111

{d}100

 

Let me know if that works for you. :)

Link to comment
Share on other sites

Very nice one!

Now I know the trouble makers :)

 

{�}194

{�}160

{ }32

 

You made my day :)

 

I'll ty to do something like

 

$string = str_replace("&#160;","",$string);

$string = str_replace("&#194;","",$string);

 

or is it \x160

 

now and see how it goes. Hopefully this will solve it, as I am not sure if it's the right format or if the special chars are the ones that need to be replaced.. Like I said, I am really bad with such things :)

Link to comment
Share on other sites

Hmm, seems like I didn't spot this as most of my "whitespaces" were at the beginning of the strings. I guess I'll have to do a str_replace on that two items then.

 

Is there a better way than doing a str_replace for each of the items that cause issues?

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.