Nelson Teixeira
Nelson Teixeira

Reputation: 6562

Simple preg_replace returns null

Why is this very simple preg_replace is returning null ?

preg_replace('[\s]+', '', "test test")

the idea was to remove spaces.

Upvotes: 0

Views: 239

Answers (3)

Toto
Toto

Reputation: 91385

Another way with delimiter [ & ]:

preg_replace('[\s+]', '', "test test")

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174706

You forget to add delimiters.

preg_replace('~\s+~', '', "test test");

And also it's better to use \s+ instead of [\s]+ in your pattern.

Upvotes: 5

Justinas
Justinas

Reputation: 43481

Your regex is wrong, there are no beginning and ending slashes:

preg_replace('/[\s]+/', '', "test test");

Upvotes: 2

Related Questions