preg_split

Im trying to get the individual date elements out of a string (it is a string, but is out of mysql, but I cannot access the MySQL date bit anymore, so can only use it as a string).

What I thought Id do is split the string using - as the split point and then pull in the array elements:-
$strDate = "2013-05-20";
echo "Year - " . preg_split('/-/', $_GET['orgValue'])[0] . "\n";
echo "Month - " . preg_split('/-/', $_GET['orgValue'])[1] . "\n";
echo "Day - " . preg_split('/-/', $_GET['orgValue'])[2] . "\n";

Open in new window


However all I get is:-
Parse error: syntax error, unexpected '[', expecting ',' or ';'

Any ideas what Im doing wrong?
tonelm54Asked:
Who is Participating?
 
Ray PaseurConnect With a Mentor Commented:
If you're working with dates, and it looks that way from the test data, this article will help answer many questions.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

You might also want to create the SSCCE for something like this.  It's not clear to me where the GET request variable comes into play.

See http://www.laprbass.com/RAY_temp_tonelm54.php

<?php // RAY_temp_tonelm54.php
error_reporting(E_ALL);
echo '<pre>';

$strDate   = "2013-05-20";
$timestamp = strtotime($strDate);
echo PHP_EOL . "YEAR: "  . date('Y', $timestamp);
echo PHP_EOL . "MONTH: " . date('m', $timestamp);
echo PHP_EOL . "DAY: "   . date('d', $timestamp);

Open in new window

The other answers are also accurate and informative, too!
0
 
käµfm³d 👽Connect With a Mentor Commented:
Why use preg_split? Would explode not suffice?

e.g.

echo "Year - " . explode('-', $_GET['orgValue'])[0] . "\n";

Open in new window

0
 
gr8gonzoConnect With a Mentor ConsultantCommented:
1. Don't use preg_split unless your delimiter is a regular expression. Use explode() instead - it's MUCH MUCH more efficient for what you're doing:

$pieces = explode('-', $value)

2. PHP has trouble evaluating the results of a function call as an array, so you have to do it in two steps:
$pieces = explode("-",$_GET["orgValue"]);
echo "Year - " . $pieces[0] . "\n";
...etc...

3. list() is your friend when using explode:
list($year,$month,$day) = explode("-",$_GET["orgValue"]);
echo "Year - " . $year . "\n";
echo "Month - " . $month . "\n";
...etc...
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
käµfm³d 👽Commented:
@gr8gonzo
Don't use preg_split unless your delimiter is a regular expression.
Technically speaking, any single character can be considered a regular expression  ; )
0
 
gr8gonzoConsultantCommented:
(Separating my post into two pieces, since I figure that someone else is going to swoop in with a quick answer while I try to type up additional suggestions)

3. Familiarize yourself with strtotime(), which will turn a string like "2013-05-13" into a UNIX timestamp, which you can then pass to date() to get any date/time portion you want:

$timestamp = strtotime($strDate);
echo "$strDate is a " . date("l",$timestamp) . ", falls in the month of " . date("F",$timestamp) . ", in week #".date("W",$timestamp)." of the year " . date("Y",$timestamp);

http://php.net/manual/en/function.date.php

This should make your date manipulation/coding much easier/more flexible.

4. To be clear on why explode() is better than preg_split() - whenever you use a preg_ function, PHP loads up a big, separate library to help it process those regular expressions. It takes up memory and a little bit of loading time. It's like renting a huge moving truck so that you can move a pillow.

When you are creating small pieces of code that will only be accessed by a couple of people, it doesn't seem like much of a difference, but when you start coding large applications, the negative effect can multiply.

5. For the sake of proper terminology, use the term "delimiter", not "split point" - you'll find that "delimiter" is used everywhere else in the coding world, so it'll make your life that much easier. :)
0
 
gr8gonzoConsultantCommented:
> Technically speaking, any single character can be considered a regular expression  ; )

Technically, yes. That's what I was getting to with my "moving truck" analogy just now. :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.