John
John

Reputation: 487

php preg_replace question.. how to get rid of a space?

$thisdate = '2011-01-18 14:52:33';

$security = preg_replace('/[^\d\s]/', '', $thisdate);

echo $security;

This results in 20110118 145233

How would i get rid of the space?

Thanks.

Upvotes: 0

Views: 65

Answers (3)

Charles Sprayberry
Charles Sprayberry

Reputation: 7853

While changing the regex is definitely the superior answer...

str_replace can also work.

$security = str_replace($security, '\s', '');

Upvotes: 0

Teneff
Teneff

Reputation: 32148

your regex matches everything that is not(^) digit(\d) or space(\s)

so if you remove the \s

preg_replace('/[^\d]/', '', $thisdate);

will replace everything except digits

Upvotes: 3

Andrea
Andrea

Reputation: 1057

take the \s out of the regex

Upvotes: 1

Related Questions