php change second field conditional

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
imadtchmn
Forum Newbie
Posts: 4
Joined: Wed Apr 14, 2010 6:42 am

php change second field conditional

Post by imadtchmn »

I am checking the second field of a variable that is separated by a space to see if it has a beginning zero. If it does, replace the zero with a space. For example:
Apr 02 2010 needs to be changed to Apr 2 2010 with double spacing between the Apr and the 2.

logic is as follows

if second field first digit = '0' then change 0 to <space> else leave it alone and continue with rest of script.
roders
Forum Commoner
Posts: 68
Joined: Tue Oct 20, 2009 9:29 am

Re: php change second field conditional

Post by roders »

If it's for date manipulation only then try this

Code: Select all

echo date("M j Y",strtotime("Apr 02 2010"));
imadtchmn
Forum Newbie
Posts: 4
Joined: Wed Apr 14, 2010 6:42 am

Re: php change second field conditional

Post by imadtchmn »

It is for a date field, however I am searching the directory for a specific date and time and the server uses the second space if the date is a single digit and the above does not give me that.
imadtchmn
Forum Newbie
Posts: 4
Joined: Wed Apr 14, 2010 6:42 am

Re: php change second field conditional

Post by imadtchmn »

this is the perl equivalent of what I am looking for.

Code: Select all

#!/usr/local/bin/perl -w

@date[0] = "Apr 04 20:22:16";
@date[1] = "Apr 14 21:22:16";
@date[2] = "Apr 04 22:23:16";
@date[3] = "Apr 24 23:22:16";
@date[4] = "Apr 04 24:22:16";


for (@date){
        if ($_ =~ /^(\w{3} 0)/){

                print "variable date is $_\n";

                $_ =~ s/^(\w{3} )0/$1 /g;

                print "variable date is now changed to: $_\n";
        }
        else {
                print "unchanged variable is $_\n";
        }
}
Last edited by Benjamin on Wed Apr 14, 2010 12:35 pm, edited 1 time in total.
Reason: Added [syntax=perl] tags.
Post Reply