Solved

Correct regex pattern for preg_split()?

Posted on 2014-10-24
2
138 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
Comment Utility
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
Comment Utility
Thank you Thank you!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

762 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

11 Experts available now in Live!

Get 1:1 Help Now