Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 163
  • Last Modified:

Correct regex pattern for preg_split()?

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
rascal
Asked:
rascal
1 Solution
 
Joshua GrantomSenior EngineerCommented:
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
 
rascalAuthor Commented:
Thank you Thank you!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now