?
Solved

preg_split

Posted on 2013-05-20
6
Medium Priority
?
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 664 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 35

Assisted Solution

by:gr8gonzo
gr8gonzo earned 668 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

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 35

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 111

Accepted Solution

by:
Ray Paseur earned 668 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
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.
Suggested Courses

764 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