Solved

Correct regex pattern for preg_split()?

Posted on 2014-10-24
2
154 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 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

617 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