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
https://forums.phpfreaks.com/topic/169001-jargons-in-array-pls-help-explain/
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;

 

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.

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.