Solved

preg_split

Posted on 2013-05-20
6
240 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 74

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 74

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 108

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses four methods for overlaying images in a container on a web page
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now