Lunatic Fnatic Lunatic Fnatic - 8 months ago 49
PHP Question

Cannot remove whitespace with preg_replace with replacing other symbols

I have a date format

as a string from my database

What I want to do is to remove dashes and other hypens and whitepaces.

I have tried this below;

$date = preg_replace('/\s+-|:/', null, $row['acctstarttime']);

//also tried this
$date = preg_replace('/\s+-|:/', '', $row['acctstarttime']);

This does not work at all. I have solved it by using this below

$date = str_replace(' ', '', preg_replace('/-|:/', null, $row['acctstarttime']));

I don't think that is a good way to do it, so how could I remove spaces while removing other symbols such as
within only using

Thanks in advance.


You can just try this:

[-, \/:]


PHP Sample


$re = '/[-, \/:]/';
$str = '2000-11-11 12-12-13';
$subst = '';

$result = preg_replace($re, $subst, $str);

echo $result;