Latest 100 public snipts »
robertbanh's
snipts » regex
showing 1-6 of 6 snipts for regex
-
∞ Dates Samples
<?php $currDate = date('Y-m-d h:i:s'); // convert timestamp from mysql $nextReviewDt = date('F d, Y',strtotime($nextReviewDt)); // Add 30 days $CommentStartDt = date('m/d/Y'); $CommentEndDt = date('m/d/Y', mktime(0,0,0,date("m"),date("d")+30,date("Y"))); // add 30 days // convert year to 4 digits if ($y > 60) $y = '19'.$y; else $y = '20'.$y; // split if there are slash or dash list($m, $d, $y) = split('[/|-]', $iDate); if (preg_match("/-/", $iDate)) list($m, $d, $y) = split('-', $iDate); else if (preg_match("/\//", $iDate)) list($m, $d, $y) = split('/', $iDate); $specDate = mktime(0, 0, 0, $m, $d, $y); $numOfDays = (time() - $specDate) / (24 * 60 * 60); // convert date from mysql db if (preg_match("/(.*)Dt$/", $dbDate)) $dbDate = date("M d, Y h:ia", strtotime($dbDate)); // output dates - walk the days $startDt = '2009-01-04'; $endDt = date('Y-m-d'); while ($startDt != $endDt) { echo "$startDt <br/>"; // increment the day $startDt = strtotime(date("Y-m-d", strtotime($startDt)) . " +1 day"); // convert back to readable format $startDt = date("Y-m-d",$startDt); } function count_days( $a, $b ) { // First we need to break these dates into their constituent parts: $gd_a = getdate( $a ); $gd_b = getdate( $b ); // Now recreate these timestamps, based upon noon on each day // The specific time doesn't matter but it must be the same each day $a_new = mktime( 12, 0, 0, $gd_a['mon'], $gd_a['mday'], $gd_a['year'] ); $b_new = mktime( 12, 0, 0, $gd_b['mon'], $gd_b['mday'], $gd_b['year'] ); // Subtract these two numbers and divide by the number of seconds in a // day. Round the result since crossing over a daylight savings time // barrier will cause this time to be off by an hour or two. return round( abs( $a_new - $b_new ) / 86400 ); }
-
∞ Regex: Date format
<? // Date must match format: mm/dd/yyyy if (!preg_match('/^\d{2}\/\d{2}\/\d{4}$/', $due_date)) echo "Error !";
-
∞ regex - remove 2 or more whitespaces
<? // remove multiple whitespaces $line1 = preg_replace("\s{2,}", " ", $line1);
-
∞ regex to return only first set
<? // extract just the first element $firstset = ereg_replace("^(.*) abc", "\\1", $data); // note: \\1 is the same as javascript's $1
-
∞ regex - replace invalid chars with underscore
<? // replace invalid chars with underscore (good for filename) $name = preg_replace("/[^a-z0-9-]/", "_" ,$name);
-
∞ PHP Regex example of greedy and lazy
<? $x = '<p id="test">p1</p> <p id="test">p2</p>'; // greedy if (preg_match('#<p id="test">(.+)</p>#', $x)) echo "greedy"; // lazy 1 if (preg_match('#<p id="test">(.+?)</p>#', $x)) echo "lazy 1"; // lazy 2 if (preg_match('#<p id="test">([^<]+)</p>#', $x)) echo "lazy 2";


