Solved

preg_split

Posted on 2013-05-20
6
244 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Password forgotten. 3 40
WordPress Themes 10 34
How do I put a session for a radio button? 4 12
geting geocode of a user with using Google maps api 8 13
This article discusses four methods for overlaying images in a container on a web page
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.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

930 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

16 Experts available now in Live!

Get 1:1 Help Now