php-general Digest 11 Nov 2011 06:13:18 -0000 Issue 7561

Topics (messages 315629 through 315635):

Re: Passing arguments to an internal function via array_map
        315629 by: Simon J Welsh
        315630 by: Marc Guay

checking dates not working
        315631 by: Marc Fromm
        315632 by: Simon J Welsh
        315633 by: tamouse mailing lists

Re: json_encode confusion
        315634 by: tamouse mailing lists

Headers already sent
        315635 by: Kranthi Krishna

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
On 11/11/2011, at 5:10 AM, Marc Guay wrote:

> Hi folks,
> 
> I'm trying to convert the contents of an array from utf8 to utf16
> (thanks for the headache MS Excel!).  I originally created a "user
> function" that just ran the text through
> mb_convert_encoding($text,'utf-16','utf-8') but now I'm wondering if I
> can't just use the internal function directly.  Apparently it's more
> complicated when the function requires arguments; I see example using
> mysql_real_escape_string() on php.net.
> 
> My current attempt looks like this:
> 
> $clean = array_map('mb_convert_encoding', $dirty_arr, 
> array('utf-16','utf-8'));
> 
> but I believe this is actually passing an array to
> mb_convert_encoding() as the second argument rather than the 2nd and
> 3rd... Any ideas or am I chasing something that can't be done?
> 
> Marc


You need to pass a second and third array to array_map() with the same number 
of elements as the first array. The arguments to the callback function are the 
elements from each array at the same offset.

Something like $clean = array_map('mb_convert_encoding', $dirty_arr, 
array_fill(0, count($dirty_arr), 'utf-16'), array_fill(0, count($dirty_arr), 
'utf-8'));
---
Simon Welsh
Admin of http://simon.geek.nz/


--- End Message ---
--- Begin Message ---
> You need to pass a second and third array to array_map() with the same number 
> of elements as the first array. The arguments to the callback function are 
> the elements from each array at the same offset.

Wow, thanks for the clarification.  I've decided to create my own
function and use that instead both because I found that there is some
other cleaning to do to the string and that code you posted is way too
convoluted to read!  :)

Marc

--- End Message ---
--- Begin Message ---
I have this bit of code to see if a date is greater or equal to a set date.

echo(date("m/d/Y",strtotime($jobs_effective_start)));// displays entered date 
of 01/03/2012
echo(date("m/d/Y",strtotime(WSOFFBEGIN))); // displays set date of 09/16/2011

if (!(date("m/d/Y",strtotime($jobs_effective_start)) >=  
date("m/d/Y",strtotime(WSOFFBEGIN)))) {
                $error.="The effective start date must be AFTER 
".WSOFFBEGIN."\n"; unset($_POST["jobs_effective_start"]);
}

My error message is displaying. The if statement is executing as true, as if 
01/03/2012 is not greater or equal to 09/16/2011.
This is not correct since a date in 2012 should be greater than a date in 2011.

If I use 12/31/2011 as the $job_effective_start date the error message is not 
displayed since 12/31/2011 is greater than 09/16/2011 and the if statement 
executes as fasle.

Any ideas on why a 2012 date is treated as not greater than a 2011 date?

Thanks

Marc

--- End Message ---
--- Begin Message ---
On 11/11/2011, at 11:35 AM, Marc Fromm wrote:

> I have this bit of code to see if a date is greater or equal to a set date.
> 
> echo(date("m/d/Y",strtotime($jobs_effective_start)));// displays entered date 
> of 01/03/2012
> echo(date("m/d/Y",strtotime(WSOFFBEGIN))); // displays set date of 09/16/2011
> 
> if (!(date("m/d/Y",strtotime($jobs_effective_start)) >=  
> date("m/d/Y",strtotime(WSOFFBEGIN)))) {
>                $error.="The effective start date must be AFTER 
> ".WSOFFBEGIN."\n"; unset($_POST["jobs_effective_start"]);
> }
> 
> My error message is displaying. The if statement is executing as true, as if 
> 01/03/2012 is not greater or equal to 09/16/2011.
> This is not correct since a date in 2012 should be greater than a date in 
> 2011.
> 
> If I use 12/31/2011 as the $job_effective_start date the error message is not 
> displayed since 12/31/2011 is greater than 09/16/2011 and the if statement 
> executes as fasle.
> 
> Any ideas on why a 2012 date is treated as not greater than a 2011 date?
> 
> Thanks
> 
> Marc


String comparisons (which is what is happening here) are done left to right. so 
it's comparing month, then day, then year. You could use a Ymd format or just 
compare the values of strtotime().
---
Simon Welsh
Admin of http://simon.geek.nz/


--- End Message ---
--- Begin Message ---
On Thu, Nov 10, 2011 at 4:35 PM, Marc Fromm <marc.fr...@wwu.edu> wrote:
> I have this bit of code to see if a date is greater or equal to a set date.
>
> echo(date("m/d/Y",strtotime($jobs_effective_start)));// displays entered date 
> of 01/03/2012
> echo(date("m/d/Y",strtotime(WSOFFBEGIN))); // displays set date of 09/16/2011
>
> if (!(date("m/d/Y",strtotime($jobs_effective_start)) >=  
> date("m/d/Y",strtotime(WSOFFBEGIN)))) {
>                $error.="The effective start date must be AFTER 
> ".WSOFFBEGIN."\n"; unset($_POST["jobs_effective_start"]);
> }

Why in the world are you comparing the formatted display dates instead
of the numeric dates set by strtotime?

if (!strtoftime($jobs_effective_start) >= strtotime(WSOFFBEGIN))

will do what you want.

Also -- why not just set WSOFFBEGIN to the converted date value
instead of converting it each time you use it? (Assuming that's a
defined constant.)

define('WSOFFBEGIN',strtotime("YYYY-MM-DD"));

or whatever.

If you need both forms (string and numeric) define two constants, one
dependent on the other.

--- End Message ---
--- Begin Message ---
On Thu, Nov 10, 2011 at 11:24 AM, Matijn Woudt <tijn...@gmail.com> wrote:
> On Thu, Nov 10, 2011 at 5:42 PM, Jim Lucas <li...@cmsws.com> wrote:
>> On 11/10/2011 6:45 AM, Bastien Koert wrote:
>>> Morning all,
>>>
>>> I've been having some fun with converting a text data file import into
>>> a json object for storage.
>>>
>>> I have a text file that is pipe delimited coming in via an upload. The
>>> first row is the headers and the second row is the data.
>>>
>>> Using this code:
>>>
>>> $data = file("inline_Nov_8_2011.txt");
>>>
>>> if(count($data)==2){
>>>
>>>       $keys   = explode("|", $data[0]);
>>>       $fields = explode("|", $data[1]);
>>>
>>>       $combine = array_combine($keys, $fields);
>>>
>>>       $json = json_encode($combine);
>>> }
>>>
>>> After the combine, I get an array that looks like this
>>>
>>> Array
>>> (
>>>     ['Legal Last Name '] => Andros
>>>     ['Legal Middle Initial '] =>
>>>     ['Legal First Name '] => Marisa
>>>     ['Maiden/Other Name '] =>
>>>     ['Social Insurance No. '] => 123456789
>>>     ['Date of Birth '] => 2/1/1988
>>>     ['Gender '] => Female
>>> )
>>>
>>> But the json encoded value looks like this (there are way more
>>> elements but this should be enough to represent what I need to do).
>>>
>>> {null:"Andros",null:"",null:"Marisa",null:"",null:"123456789",null:"2\/1\/1988",null:"Female"}
>>>
>>> I have been googling for info about allowed values for the json keys,
>>> but can't seem to find a clear doc on what is allowed and what isn't.
>>> I have tried unquoted keys, replaced the spaced with underscores but
>>> nothing I do seems to help.
>>>
>>> When I echo out the json encoded data, the keys are all nulls.
>>>
>>> Can someone point me in the correct direction? It may be that I need
>>> to manually create the key names as an array first, which I was hoping
>>> to avoid since the file format coming from the client is still in some
>>> flux.
>>>
>>
>> Looking at your input array example, I see that you have trailing spaces in 
>> your
>> keys.  Looking at the working example of DiRaven, the only difference that I 
>> see
>> with his example is that the input key values do not have a trailing spaces.
>> json is probably having an issue with that trailing spaces.
>>
>
> That might be a possibility, but I suspect that the single quotes
> around the keys might be a problem. I haven't tried it though.
>
> Matijn
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Maybe use preg_split instead of explode? '/\s*|\s*/' -- assuming the
spaces between fields aren't important...

--- End Message ---
--- Begin Message ---
Hi all,

I am missing something pretty obvious here. The PHP Manual says
"Remember that header() must be called before any actual output is
sent, either by normal HTML tags, blank lines in a file, or from
PHP.". A simple test case shows otherwise

I have the following code

test
<?php
setcookie("TestCookie", 'test');

nothing more nothing less.. only this code in a file

I get the output in the browser ("test") AND the cookie is set
(verified by live HTTP headers). Any ides on why this is happening ?

Linux localhost 2.6.40-4.fc15.i686 #1 SMP Fri Jul 29 18:54:39 UTC 2011 i686
Apache 2.0 Handler
Zend Engine v2.3.0,  Xdebug v2.1.2
PHP 5.3.6

Kranthi.
http://goo.gl/e6t3

--- End Message ---

Reply via email to