Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Correct regex pattern for preg_split()?

Posted on 2014-10-24
2
Medium Priority
?
161 Views
Last Modified: 2014-10-24
I am going crazy trying to figure out the correct regex pattern for a preg_split().

I tried using the examples on various pages but none of them worked.

I wish to split a string that by either a period (.) a slash (/) or a dash (-).

The input string in this case is 2014-10-15

The code that fails is:

    
$mmddyy = '10-31-2014';
$pattern = '[/.-]';
	$arr = preg_split($pattern, $mmddyy);
    $month = $arr[0];
    $day = $arr[1];
    $year = $arr[2];
    mytrace("month=$month day=$day year=$year");

Open in new window


When mytrace displays the month day and year, it displays as:
month=10-31-2014 day= year=

So clearly the preg_split() regex syntax I am using is incorrect.

Can someone please put me out of my misery? :)
0
Comment
Question by:rascal
[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
2 Comments
 
LVL 16

Accepted Solution

by:
Joshua Grantom earned 2000 total points
ID: 40402768
Here you go. Either one of these should work

This one escapes the "." just in case it tries to match anything, makes it a literal "." period even though the brackets should force it anyway.
$pattern = '/[-\/\.]/';

Open in new window

Without the escaped "." period
$pattern = '/[-\/.]/';

Open in new window

0
 
LVL 1

Author Closing Comment

by:rascal
ID: 40402903
Thank you Thank you!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
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.

722 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