0

I literally spent 6 hours trying to find this out. I have the code below:

$pieces3 = explode(":", $value2);
$checkiftime = $pieces3[0];
if ($checkiftime == 'time')
{
echo "Yes!";
}
else
{
echo "Oh no!";
}

$value2 contains:

" 'time': 1376952971"

I'm pretty sure the statement should be true but for some reasons, I keep getting false (oh no!)

if ($checkiftime == 'time')

What could be wrong? I already tried using double quotes for $checkiftime=="time" but still no avail.

EDIT:

I tried doing Vardumps after the explode:

var_dump($pieces3);
echo "<br>";
var_dump($value2);
echo "<br>";
var_dump($pieces3[0]);
echo "<br>";

And resulted with:

array(2) { [0]=> string(13) " 'time'" [1]=> string(11) " 1376952971" } 
string(25) " 'time': 1376952971" 
string(13) " 'time'" 

I'm not sure why 'time' shows as String(13), I see that blank space, could that be a special character containing more than 1 hidden string? I did a trim() but it doesn't seem to change anything.

EDIT2:

Thanks everyone, below is the part of the code that I modified and it worked great:

$checkiftime = trim($pieces3[0], " '\t\n\r\0\x0B");
if ($checkiftime == 'time')
12
  • 3
    var_dump($pieces3); - Can you post result formatted in question please? Commented May 7, 2014 at 22:47
  • 4
    Just ran your exact code, using $value2 = "time: 1376952971" and it works as expected. The result is Yes! Commented May 7, 2014 at 22:50
  • 1
    codepad.org/yGutpX7h Commented May 7, 2014 at 22:52
  • 1
    have you tried trim($checkiftime) before comparing it? Commented May 7, 2014 at 23:14
  • 1
    +1 @MarkM. It also works in many a PHP versions: 3v4l.org/sZo1U Commented May 8, 2014 at 0:48

5 Answers 5

2

I guess the main problem is that $value2 contained an unexpected value.

It simply looks like you need to sanitise the string, removing any characters that might get in the way of your check (such as space and apostrophe / single-quote). Try this...

$checkiftime = trim($pieces3[0], " '\t\n\r\0\x0B");

See trim(). Demo here - http://ideone.com/uVEDXM

Alternatively, you could simply use a regular expression match to check the entire string, eg

if (preg_match('/^\W*time\W*: (\d+)$/', $value2, $matches)) {
    echo 'Yes!', PHP_EOL;
    var_dump($matches); // $matches[1] contains the numeric value 1376952971
}

Demo - http://ideone.com/025vAV

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Phil. That would only remove the ' before the word time wouldn't it? How about the ' after the word time? Also is that a regular blank space before the word 'time'? I'm asking because it is showing as String(13) when it only contains 7 characters, so I'm thinking that blank space consist of 6 characters.
@user3613603 Read the manual for trim(), it removes leading and trailing characters. You are correct about the string length being longer than expected though the information you've pasted into your question only contains a normal space character. I've updated my answer to be more comprehensive
Hi Phil, the $checkiftime = trim($pieces3[0], " '\t\n\r\0\x0B"); did the trick. So Ibelieve that blank space is a special character? Probably counting as more than 1 character? It sounds weird but your code worked so that's the only explanation I can think of
0

I'm not sure why you have a space in the beginning of your 'time' or why you're keeping the single quotes around it, but here is the code that will produce a yes based on how it looks like you're setting it up: http://codepad.org/T8yw8Ou5

Consider using JSON for a situation like this!

1 Comment

Hi Ramsey, that is actually a piece of data from a JSON log file. I just used explode() to strip out the " 'time': 215416541654" and will be converting that unix timestamp to date format
0
$value2 = " 'time': 1376952971";

if( stristr( $value2 ,'time' ) === FALSE )
{
    echo "Oh no!";
}
else
{
    echo "Yes!";
}

The above checks if string time is present in the $value2 . But if you need the time you could :

echo ( stristr( $value2 ,"'time':" ) === FALSE
        ? ''
        : trim( str_replace( "'time':" ,'' ,$value2 ) )
     );

3 Comments

This look's interesting, correct me if I'm wrong but this checks if the word "time" can be found in $value2 string?
Sure , it finds the first occurrence of the string time in $value2 . stristr .
You could try this : echo ( stristr( $value2 ,"'time':" ) === FALSE ? '' : trim( str_replace( "'time':" ,'' ,$value2 ) ) );
0

This appears to be part of some JSON data, except that it's malformed in that it uses single-quoted strings (which is valid in JavaScript but not valid in JSON). If we crudely replace single quotes with double quotes, and wrap it in {} to make it a complete object, it's easy to parse this string with json_decode(), which will handle variations in formatting and space characters that blunt splitting with explode() cannot:

$value = " 'time': 1376952971";
$value = json_decode('{' . strtr($value, "'", '"') . '}');
echo $value->time;
// Outputs: 1376952971

6 Comments

Actually, it IS a JSON file. I just replaced the double quotes as it is part of my process
This is actually interesting, got a question though. What if you're given this JSON string:
This is actually interesting, got a question though. What if you're given this JSON string: "Team_Items":{ "Army":{ "Paladins":{ "325648":0, "546545":4 }, "Knights":{ "325648":-2, "546545":0 } } } Can you use json_decode and get these items and values and their sub items and values and print them?
can I do echo $value->325648; result is 0 or echo $value->Paladins; result is "325648":0, "546545":4
@user3613603 Yes, although that data must be wrapped in an outer {} to be a complete JSON string. If you have that string in $value then you can do: $value = json_decode('{' . $value . '}');. And then echo $value->Team_Items->Army->Paladins->{546545}; will display 4, and print_r($value); will echo the whole structure.
|
-1

Your time value has single quotes around it, within the string. So, you'd need to make your comparison:

$checkiftime = trim($checkiftime, " \t\n\r\0\x0B");
if ($checkiftime == "'time'") {
...
}

This is probably a good lesson in examining your values before asking a question about it :)

You can remove the single quotes if you want:

$checkiftime = str_replace("'", "", $checkiftime);

7 Comments

There's also some whitespace characters though it seems they are more than simply ASCII space
@Phil That's what the trim() is for
This did not work for me. I should probably post the entire code but it contains sensitive information so I might need to modify it. I tried trim, the result is the same.
Did you copy the == "'time'" exactly (including the single quotes), and put the trim() function before it?
Also, if you put the str_replace function in there, before the comparison, you need to remove the single quotes from the == "'time'" portion
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.