shamuraq Posted August 5, 2009 Share Posted August 5, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/169001-jargons-in-array-pls-help-explain/ Share on other sites More sharing options...
Zane Posted August 5, 2009 Share Posted August 5, 2009 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; Quote Link to comment https://forums.phpfreaks.com/topic/169001-jargons-in-array-pls-help-explain/#findComment-891691 Share on other sites More sharing options...
nrg_alpha Posted August 5, 2009 Share Posted August 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/169001-jargons-in-array-pls-help-explain/#findComment-891700 Share on other sites More sharing options...
.josh Posted August 5, 2009 Share Posted August 5, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/169001-jargons-in-array-pls-help-explain/#findComment-891738 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.