• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
preg_replace help needed

#1
[eluser]CISCK[/eluser]
So, I have a comma delimitated string of 24 hour times:

Code:
$hours = "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23";

I am matching each number in the string using:

Code:
$pattern = '/\d+/';

What would be the correct replacement to achieve:

12am, 1am, 2am, 3am, 4am, 5am, 6am, 7am, 8am, 9am, 10am, 11am, 12pm, 1pm , 2pm, 3pm, 4pm, 5pm, 6pm, 7pm, 8pm, 9pm, 10pm, 11pm

Thanks!

--

Code:
$hours = "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23";
$pattern = '/\d+/';
$replacement = 'PLEASE HELP!';
preg_replace($pattern, $replacement, $hours);

#2
[eluser]jedd[/eluser]
It's always so much more challenging when you read a message and have to reverse engineer the actual problem that someone is trying to solve.

It seems to me that you had one problem - you have a list of 24 numbers that you want to map to a list of 24 strings.

For some reason you then thought you should use regex. Now you have two problems.

So .. assuming all you really want to do is get from this:
Code:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23

to this:
Code:
12am, 1am, 2am, 3am, 4am, 5am, 6am, 7am, 8am, 9am, 10am, 11am, 12pm, 1pm , 2pm, 3pm, 4pm, 5pm, 6pm, 7pm, 8pm, 9pm, 10pm, 11pm

Why don't you just have this:
Code:
$twentyfour = array (
          0 => "12am",
          1 => "1am",
          2 => "2am",
          ...
          23 => "11pm",
          );

Cycling through $key, cycling through $value and doing lookups are easy with this array.

#3
[eluser]CISCK[/eluser]
I guess I just wanted something more dynamic for replacing the $hours string with the corresponding values (e.g. the character 0 is replaced with 12am), but this could work. Thanks for the help!

#4
[eluser]jedd[/eluser]
[quote author="CISCK" date="1257020493"]
I guess I just wanted something more dynamic ...
[/quote]

Well, sure, if you want something that takes up more space, uses more CPU to run, and whose function is less obvious to the reader .. you don't have to use a simple and effective solution.

Here's a function I wrote a while ago that includes a 24hour conversion similar to what you're trying to do. In this case I wanted to keep the date portion of a string, and round the hours/minutes/seconds portion to the nearest hour.

Code:
// ------------------------------------------------------------------------
/**
* Pretty date
*
* Takes one parameter - an ISO8601 formatted date - and returns it
* in slightly more human readable form.
*
* eg. 2009-05-31T16:42:07  ====>  2009-05-31 , 5pm
*
* @param    $date_in    string (iso8601 date)
* @return    string
**/
function  pretty_date ( $date_in )  {
    if (strlen ($date_in) != 19)
        return $date_in;                // return in confusion!

    $hh = substr ($date_in, 11, 2);
    $mm = substr ($date_in, 14, 2);

    if ($mm > 30)
        $hh++;

    if ( ($hh > 23) OR ($hh == '00'))
        $hh_string = "midnight";
    else
        if ($hh > 12)
            $hh_string = ($hh - 12) ."pm";
        else
            if ($hh == 12)
                $hh_string = "midday";
            else
                $hh_string = $hh ."am";

    $output = substr ($date_in, 0, 10) . nbs(1) ."<font class=\"date_tilde\">~</font>". nbs(1) . $hh_string;

    return $output;
    }  // end-function  pretty_date ()


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.