Solved

preg_split

Posted on 2013-05-20
6
255 Views
Last Modified: 2013-06-03
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?
0
Comment
Question by:tonelm54
  • 3
  • 2
6 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 166 total points
ID: 39181143
Why use preg_split? Would explode not suffice?

e.g.

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

Open in new window

0
 
LVL 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 167 total points
ID: 39181155
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39181160
@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
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.

 
LVL 34

Expert Comment

by:gr8gonzo
ID: 39181183
(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
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 39181185
> 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
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 167 total points
ID: 39181201
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

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question