Jump to content

Jargons in array... Pls help explain


shamuraq

Recommended Posts

i saw this weird looking example when trying to learn preg_replace() function:

<?php
$patterns = array ('/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/',
                   '/^\s*{(\w+)}\s*=/');
$replace = array ('\3/\4/\1\2', '$\1 =');
echo preg_replace($patterns, $replace, '{startDate} = 1999-5-27');

?>

Output:

5/27/1999

Can anyone pls explain what the jargons in $pattern and $replace means?

 

Link to comment
Share on other sites

that jargon is called regex...  this particular flavor is PCRE...the other is called POSIX

 

In short...all the script does is take an input of YYYY-MM-DD and output it as MM/DD/YYYY

 

this can easily be done a simpler way..such as

$startDate = '2008-03-24';
$startDate = date('n/j/Y',strtotime($startDate));
echo $startDate;

 

Link to comment
Share on other sites

You can learn more about pcre in that link, as well as the following resources:

 

http://www.regular-expressions.info/

http://weblogtoolscollection.com/regex/regex.php

http://www.phpfreaks.com/forums/index.php/topic,127902.0.html (obviously, there is more if you google it).

 

That should be enough to get you started.

Link to comment
Share on other sites

that jargon is called regex...  this particular flavor is PCRE...the other is called POSIX

 

In short...all the script does is take an input of YYYY-MM-DD and output it as MM/DD/YYYY

 

this can easily be done a simpler way..such as

$startDate = '2008-03-24';
$startDate = date('n/j/Y',strtotime($startDate));
echo $startDate;

 

 

couple things with that though:

 

1) it does not preserve original (lack of) 0 padding

2) it only addresses the date part of that regex

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.