Mcod Posted January 12, 2012 Share Posted January 12, 2012 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. Quote Link to comment Share on other sites More sharing options...
Mcod Posted January 12, 2012 Author Share Posted January 12, 2012 I forgot to mention that I also tried replacing \xa0 and also   just in case, so it's not the well known "invisible" Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 12, 2012 Share Posted January 12, 2012 What problem are you trying to solve? Quote Link to comment Share on other sites More sharing options...
ragax Posted January 12, 2012 Share Posted January 12, 2012 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. Quote Link to comment Share on other sites More sharing options...
Mcod Posted January 12, 2012 Author Share Posted January 12, 2012 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 Quote Link to comment Share on other sites More sharing options...
Mcod Posted January 12, 2012 Author Share Posted January 12, 2012 Here is an image with ISO-8859-1 header and without utf8_decode so you can see how the A looks. Quote Link to comment Share on other sites More sharing options...
ragax Posted January 12, 2012 Share Posted January 12, 2012 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. Quote Link to comment Share on other sites More sharing options...
Mcod Posted January 12, 2012 Author Share Posted January 12, 2012 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(" ","",$string); $string = str_replace("Â","",$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 Quote Link to comment Share on other sites More sharing options...
ragax Posted January 12, 2012 Share Posted January 12, 2012 Fantastic. The manual's ord page also has an example with a utfCharToNumber function that might work better in some cases. Quote Link to comment Share on other sites More sharing options...
requinix Posted January 12, 2012 Share Posted January 12, 2012 #194#160 is the UTF-8 encoding of a non-breaking space. So decode it first, remove the whitespace, and re-encode it. Quote Link to comment Share on other sites More sharing options...
Mcod Posted January 13, 2012 Author Share Posted January 13, 2012 Thanks for all your help, I managed to get rid of the whitespaces with the following line: $mystring = trim($mystring, "\xC2\xA0\n"); Again, thanks for helping me find a solution! Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 13, 2012 Share Posted January 13, 2012 You said you wanted to remove all whitespace. That will only remove leading and trailing whitespace. Quote Link to comment Share on other sites More sharing options...
Mcod Posted January 14, 2012 Author Share Posted January 14, 2012 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.