Solved

Correct regex pattern for preg_split()?

Posted on 2014-10-24
2
140 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
2 Comments
 
LVL 16

Accepted Solution

by:
Joshua Grantom earned 500 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

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
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.

919 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