Anna K
Anna K

Reputation: 1774

How to trim a value in php?

I'm trying to take a value from some variable which looks like this:

$ a= '[custom:data-rekrutacji]';

using [custom:data-rekrutacji] token which I've already defined which should contain the value: 2015-08-11 13:00:00, but if I check it using var_dump() as below:

var_dump($a);

I'm getting something like this:

string(24) "
2015-08-11 13:00:00

"

However I would like to have only this (as below) so I can use function strtotime($a):

2015-08-11 13:00:00

Is it possible to trim this?

I've tried in this way, but without success:

$a= '[custom:data-rekrutacji]';
trim($a);
echo strtotime($a);

Upvotes: 0

Views: 1097

Answers (2)

kenorb
kenorb

Reputation: 166339

When using trim() function, remember it returns a string, not changing the existing one. So to make it work, you should do:

$a = trim($a);

instead of just:

trim($a);

As this function returns a string with whitespace stripped from the beginning and end of str.

Upvotes: 0

Kevin P.
Kevin P.

Reputation: 401

Have you tried saving the change:

$a = trim($a);

$a= '[custom:data-rekrutacji]';
$a = trim($a);
echo strtotime($a);

EDIT:

If your variable has the correct values it should output correctly.

$a= "    2015-08-11 13:00:00    ";
$a = trim($a);
echo "Trimmed: [".$a."]\n";
echo strtotime($a);

Code between the [ ] shows it was trimmed correctly, the next one should return the time in an int.

This is the result I get:

Trimmed: [2015-08-11 13:00:00]
1439290800

Upvotes: 2

Related Questions