Lunatic Fnatic Lunatic Fnatic - 1 year ago 78
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.

Answer Source

You can just try this:

[-, \/:]


PHP Sample


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

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

echo $result;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download