84

I know this comment PHP.net. I would like to have a similar tool like tr for PHP such that I can run simply

tr -d " " ""

I run unsuccessfully the function php_strip_whitespace by

$tags_trimmed = php_strip_whitespace($tags);

I run the regex function also unsuccessfully

$tags_trimmed = preg_replace(" ", "", $tags);
Mohammad
  • 21,175
  • 15
  • 55
  • 84
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
  • possible duplicate of [Can't get str\_replace() to strip out spaces in a PHP string](http://stackoverflow.com/questions/16563421/cant-get-str-replace-to-strip-out-spaces-in-a-php-string) – T.Todua Mar 25 '14 at 00:37
  • 1
    @taztodgmail Look at the dates. This is 2009 and the other is 2013. The 2013 is duplicate of this. – Léo Léopold Hertz 준영 Mar 25 '14 at 19:02
  • 1
    FYI: the \s flag does not included utf-8 characters such as utf-8 encoded nbsp;, ps, quads and fs http://stackoverflow.com/questions/2227921/simplest-way-to-get-a-complete-list-of-all-the-utf-8-whitespace-characters-in-ph – ppostma1 Oct 14 '16 at 15:38
  • 1
    sure! I have a set of code that addresses it – ppostma1 Oct 14 '16 at 17:04
  • Why is this popping up in the front page? – Jonathan DS Oct 21 '19 at 18:21

15 Answers15

135

To strip any whitespace, you can use a regular expression

$str=preg_replace('/\s+/', '', $str);

See also this answer for something which can handle whitespace in UTF-8 strings.

Community
  • 1
  • 1
Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
53

A regular expression does not account for UTF-8 characters by default. The \s meta-character only accounts for the original latin set. Therefore, the following command only removes tabs, spaces, carriage returns and new lines

// http://stackoverflow.com/a/1279798/54964
$str=preg_replace('/\s+/', '', $str);

With UTF-8 becoming mainstream this expression will more frequently fail/halt when it reaches the new utf-8 characters, leaving white spaces behind that the \s cannot account for.

To deal with the new types of white spaces introduced in unicode/utf-8, a more extensive string is required to match and removed modern white space.

Because regular expressions by default do not recognize multi-byte characters, only a delimited meta string can be used to identify them, to prevent the byte segments from being alters in other utf-8 characters (\x80 in the quad set could replace all \x80 sub-bytes in smart quotes)

$cleanedstr = preg_replace(
    "/(\t|\n|\v|\f|\r| |\xC2\x85|\xc2\xa0|\xe1\xa0\x8e|\xe2\x80[\x80-\x8D]|\xe2\x80\xa8|\xe2\x80\xa9|\xe2\x80\xaF|\xe2\x81\x9f|\xe2\x81\xa0|\xe3\x80\x80|\xef\xbb\xbf)+/",
    "_",
    $str
);

This accounts for and removes tabs, newlines, vertical tabs, formfeeds, carriage returns, spaces, and additionally from here:

nextline, non-breaking spaces, mongolian vowel separator, [en quad, em quad, en space, em space, three-per-em space, four-per-em space, six-per-em space, figure space, punctuation space, thin space, hair space, zero width space, zero width non-joiner, zero width joiner], line separator, paragraph separator, narrow no-break space, medium mathematical space, word joiner, ideographical space, and the zero width non-breaking space.

Many of these wreak havoc in xml files when exported from automated tools or sites which foul up text searches, recognition, and can be pasted invisibly into PHP source code which causes the parser to jump to next command (paragraph and line separators) which causes lines of code to be skipped resulting in intermittent, unexplained errors that we have begun referring to as "textually transmitted diseases"

[Its not safe to copy and paste from the web anymore. Use a character scanner to protect your code. lol]

Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
ppostma1
  • 3,616
  • 1
  • 27
  • 28
  • 1
    Like that? I tried to explain the the old one doesn't work like it should anymore – ppostma1 Oct 14 '16 at 21:26
  • Excellent! Can you please include the count of the characters you have in your set? - - This will help us to identify that you really have a complete set of those characters. - - I accepted your answer becouse it is now more complete than the old accepted answer. – Léo Léopold Hertz 준영 Oct 14 '16 at 21:40
  • Thanks for the help @Masi! – ppostma1 Oct 18 '16 at 14:43
  • 1
    Thanks, **BUT using `trim()` should be the right answer** – Husam Mar 23 '17 at 15:34
  • 1
    Shouldn't there be the `u` flag in the regex? –  Aug 19 '17 at 16:51
  • That is not necessary because it uses fixed strings and alternators: | If it were done properly in a character set such as [\t\n\r\xC2\x85\xa0] then it would need a /u to signify the utf-8 string is multi-byte. (or it would strip every individual occurrence of \xC2, \x85, and \xa0 from the source text). But the character set version is not as understandable to read. – ppostma1 Aug 23 '17 at 16:31
  • Note that you can omit the `+` sign from the regex, this would be the same: `preg_replace('/\s/', '', $str);` – Tim Visée May 29 '19 at 19:59
27

Sometimes you would need to delete consecutive white spaces. You can do it like this:

$str = "My   name    is";
$str = preg_replace('/\s\s+/', ' ', $str);

Output:

My name is
trante
  • 33,518
  • 47
  • 192
  • 272
16
$string = str_replace(" ", "", $string);

I believe preg_replace would be looking for something like [:space:]

Tyler Carter
  • 60,743
  • 20
  • 130
  • 150
8

You can use trim function from php to trim both sides (left and right)

 trim($yourinputdata," ");

Or

trim($yourinputdata);

You can also use

ltrim() - Removes whitespace or other predefined characters from the left side of a string
rtrim() - Removes whitespace or other predefined characters from the right side of a string

System: PHP 4,5,7
Docs: http://php.net/manual/en/function.trim.php

Kumar
  • 1,187
  • 18
  • 31
  • 1
    This should be the correct answer as it does natively, exactly what is asked by the OP. It is small, clean and 'expandable' by adding extra char to be trimmed. – Louis Loudog Trottier Feb 11 '17 at 14:04
  • 1
    Trim doesn't remove spaces INSIDE the string, only on left and right. BTW, space is default in trim, you don't have to give the 2nd argument –  Aug 19 '17 at 16:53
6

If you want to remove all whitespaces everywhere from $tags why not just:

str_replace(' ', '', $tags);

If you want to remove new lines and such that would require a bit more...

camomileCase
  • 1,706
  • 2
  • 18
  • 27
2

Any possible option is to use custom file wrapper for simulating variables as files. You can achieve it by using this:

1) First of all, register your wrapper (only once in file, use it like session_start()):

stream_wrapper_register('var', VarWrapper);

2) Then define your wrapper class (it is really fast written, not completely correct, but it works):

class VarWrapper {
  protected $pos = 0;
  protected $content;
  public function stream_open($path, $mode, $options, &$opened_path) {
    $varname = substr($path, 6);
    global $$varname;
    $this->content = $$varname;
    return true;
  }
  public function stream_read($count) {
    $s = substr($this->content, $this->pos, $count);
    $this->pos += $count;
    return $s;
  }
  public function stream_stat() {
    $f = fopen(__file__, 'rb');
    $a = fstat($f);
    fclose($f);
    if (isset($a[7])) $a[7] = strlen($this->content);
    return $a;
  }
}

3) Then use any file function with your wrapper on var:// protocol (you can use it for include, require etc. too):

global $__myVar;
$__myVar = 'Enter tags here';
$data = php_strip_whitespace('var://__myVar');

Note: Don't forget to have your variable in global scope (like global $__myVar)

micropro.cz
  • 598
  • 4
  • 17
  • 1
    This is complex, beginners might not wish to take a look at this. But for showing effort I'll upvote you so that the -1 in this post will be removed – Ironwind Sep 18 '13 at 05:50
  • 1
    Yes, I know it is very complex, but it works and in some cases it is really powerful. And unfortunelly it is the only way (instead of creating tempfiles, which is ugly) how to send variable to functions which works only with files (and php_strip_whitespace is not the only one). For example, you can substitute code before require - you can create own "pre-compiler" for PHP where you can do whatever you want. I use it and it becomes very powerful and useful during years of programming. – micropro.cz Sep 24 '13 at 14:19
  • 1
    `global $$varname;` How horrible it was in 2013 – B001ᛦ Jul 02 '20 at 18:21
2

This is an old post but the shortest answer is not listed here so I am adding it now

strtr($str,[' '=>'']);

Another common way to "skin this cat" would be to use explode and implode like this

implode('',explode(' ', $str));

Yehuda Schwartz
  • 3,378
  • 3
  • 29
  • 38
1

You can do it by using ereg_replace

 $str = 'This Is New Method Ever';
 $newstr = ereg_replace([[:space:]])+', '',  trim($str)):
 echo $newstr
 // Result - ThisIsNewMethodEver
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
1

you also use preg_replace_callback function . and this function is identical to its sibling preg_replace except for it can take a callback function which gives you more control on how you manipulate your output.

$str = "this is a   string";

echo preg_replace_callback(
        '/\s+/',
        function ($matches) {
            return "";
        },
        $str
      );
samehanwar
  • 3,280
  • 2
  • 23
  • 26
1
$string = trim(preg_replace('/\s+/','',$string));
Mert Simsek
  • 700
  • 7
  • 12
0

Is old post but can be done like this:

if(!function_exists('strim')) :
function strim($str,$charlist=" ",$option=0){
    $return='';
    if(is_string($str))
    {
        // Translate HTML entities
        $return = str_replace(" "," ",$str);
        $return = strtr($return, array_flip(get_html_translation_table(HTML_ENTITIES, ENT_QUOTES)));
        // Choose trim option
        switch($option)
        {
            // Strip whitespace (and other characters) from the begin and end of string
            default:
            case 0:
                $return = trim($return,$charlist);
            break;
            // Strip whitespace (and other characters) from the begin of string 
            case 1:
                $return = ltrim($return,$charlist);
            break;
            // Strip whitespace (and other characters) from the end of string 
            case 2:
                $return = rtrim($return,$charlist);
            break;

        }
    }
    return $return;
}
endif;

Standard trim() functions can be a problematic when come HTML entities. That's why i wrote "Super Trim" function what is used to handle with this problem and also you can choose is trimming from the begin, end or booth side of string.

Ivijan Stefan Stipić
  • 6,249
  • 6
  • 45
  • 78
0

A simple way to remove spaces from the whole string is to use the explode function and print the whole string using a for loop.

 $text = $_POST['string'];
            $a=explode(" ", $text);
            $count=count($a);
            for($i=0;$i<$count; $i++){

                echo $a[$i];
            }
mburesh
  • 1,012
  • 5
  • 15
  • 22
Dhruv Thakkar
  • 415
  • 1
  • 6
  • 18
0

The \s regex argument is not compatible with UTF-8 multybyte strings.

This PHP RegEx is one I wrote to solve this using PCRE (Perl Compatible Regular Expressions) based arguments as a replacement for UTF-8 strings:

function remove_utf8_whitespace($string) { 
   return preg_replace('/\h+/u','',preg_replace('/\R+/u','',$string)); 
}

- Example Usage -

Before:

$string = " this is a test \n and another test\n\r\t ok! \n";

echo $string;

 this is a test
 and another test
         ok!

echo strlen($string); // result: 43

After:

$string = remove_utf8_whitespace($string);

echo $string;

thisisatestandanothertestok!

echo strlen($string); // result: 28

PCRE Argument Listing

Source: https://www.rexegg.com/regex-quickstart.html

Character   Legend  Example Sample Match
\t  Tab T\t\w{2}    T     ab
\r  Carriage return character   see below   
\n  Line feed character see below   
\r\n    Line separator on Windows   AB\r\nCD    AB
    CD
\N  Perl, PCRE (C, PHP, R…): one character that is not a line break \N+ ABC
\h  Perl, PCRE (C, PHP, R…), Java: one horizontal whitespace character: tab or Unicode space separator      
\H  One character that is not a horizontal whitespace       
\v  .NET, JavaScript, Python, Ruby: vertical tab        
\v  Perl, PCRE (C, PHP, R…), Java: one vertical whitespace character: line feed, carriage return, vertical tab, form feed, paragraph or line separator      
\V  Perl, PCRE (C, PHP, R…), Java: any character that is not a vertical whitespace      
\R  Perl, PCRE (C, PHP, R…), Java: one line break (carriage return + line feed pair, and all the characters matched by \v)      
Jeff Clayton
  • 7,053
  • 1
  • 31
  • 36
0

There are some special types of whitespace in the form of tags. You need to use

$str=strip_tags($str);

to remove redundant tags, error tags, to get to a normal string first.

And use

$str=preg_replace('/\s+/', '', $str);

It's work for me.

Musa
  • 1,334
  • 1
  • 11
  • 21