Jorge
Jorge

Reputation: 5676

PHP string remove space

Is there php function to remove the space inside the string? for example:

$abcd="this is a test"

I want to get the string:

$abcd="thisisatest"

How to do that?

Upvotes: 4

Views: 8334

Answers (3)

Fury
Fury

Reputation: 4776

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

Upvotes: 0

Gordon
Gordon

Reputation: 317177

$abcd = str_replace(' ', '', 'this is a test');

See http://php.net/manual/en/function.str-replace.php

Upvotes: 16

Elitmiar
Elitmiar

Reputation: 36899

The following will also work

$abcd="this is a test";
$abcd = preg_replace('/( *)/', '', $abcd);
echo $abcd."\n"; //Will output 'thisisatest';

or

$abcd = preg_replace('/\s/', '', $abcd);

See manual http://php.net/manual/en/function.preg-replace.php

Upvotes: 3

Related Questions