i
When this modifier is used, the matching of alphabetic characters in the pattern becomes non-case-sensitive; for example, "/sgi/i" matches both "sgi" and "SGI." This is equivalent to Perl's /i modifier.
m
By default, PCRE treats the subject string as consisting of a single "line" of characters (even if it actually contains several newlines). The "start of line" metacharacter (^) matches only at the start of the string, while the "end of line" metacharacter ($) matches only at the end of the string, or before a terminating newline (unless the D modifier is also set). This is the same as in Perl.
When this modifier is used, the "start of line" and "end of line" constructs match immediately following or immediately before any newline in the subject string, respectively, as well as at the very start and end. This is equivalent to Perl's /m modifier. If there are no "\n" characters in a subject string, or no occurrences of ^ or $ in a pattern, setting this modifier has no effect.
s
When this modifier is used, a dot metacharacter (.) in the pattern matches all characters, including newlines. Without it, newlines are excluded. This modifier is equivalent to Perl's /s modifier. A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.
x
When this modifier is used, whitespace data characters in the pattern are ignored except when escaped or inside a character class, and characters between an unescaped # outside a character class and the next newline character, inclusive, are also ignored. This is equivalent to Perl's /x modifier, and makes it possible to include comments inside complicated patterns. Note, however, that this applies only to data characters. Whitespace characters cannot appear within special character sequences in a pattern, for example within the sequence (?(, which introduces a conditional subpattern.
e
When this modifier is used, preg_replace() does normal substitution of references in the replacement string, evaluates it as PHP code, and uses the result of the evaluation for replacing the match found by the pattern.
Only preg_replace() uses this modifier; it's ignored by other PCRE functions.
A
When this modifier is used, the pattern is forced to be "anchored"; that is, it's constrained to match only at the start of the string that's being searched (the "subject string"). This effect can also be achieved by appropriate constructs in the pattern itself, which is the only way to do it in Perl.
D
When this modifier is used, a dollar metacharacter ($) in the pattern matches only at the end of the subject string. Without this modifier, a dollar sign also matches immediately before the final character if it's a newline (but not before any other newlines). This modifier is ignored if the /m modifier is set. There is no equivalent to this modifier in Perl.
S
When a pattern is going to be used several times, it's worth spending more time analyzing it in order to speed up the time taken for matching. When this modifier is used, this extra analysis is performed. At present, studying a pattern is useful only for non-anchored patterns that don't have a single fixed starting character. This is equivalent to the study() function in Perl.
U
This modifier inverts the "greediness" of the quantifiers so that they're not greedy by default, but become greedy if followed by "?". Greedy quantifiers attempt to match as much of the target string as they legally can. The only limit on this behavior is that the greediness of one quantifier cannot cause the following other quantifiers in the pattern to fail. This modifier is not compatible with Perl.
X
This modifier turns on additional functionality of PCRE that is incompatible with Perl. Any backslash in a pattern that's followed by a letter that has no special meaning causes an error, thus reserving these combinations for future expansion. By default, as in Perl, a backslash followed by a letter with no special meaning is treated as a literal. At present, no other features are controlled by this modifier.
Monday, October 26, 2009
Regex pattern modifier -PHP/Perl
i
When this modifier is used, the matching of alphabetic characters in the pattern becomes non-case-sensitive; for example, "/sgi/i" matches both "sgi" and "SGI." This is equivalent to Perl's /i modifier.
m
By default, PCRE treats the subject string as consisting of a single "line" of characters (even if it actually contains several newlines). The "start of line" metacharacter (^) matches only at the start of the string, while the "end of line" metacharacter ($) matches only at the end of the string, or before a terminating newline (unless the D modifier is also set). This is the same as in Perl.
When this modifier is used, the "start of line" and "end of line" constructs match immediately following or immediately before any newline in the subject string, respectively, as well as at the very start and end. This is equivalent to Perl's /m modifier. If there are no "\n" characters in a subject string, or no occurrences of ^ or $ in a pattern, setting this modifier has no effect.
s
When this modifier is used, a dot metacharacter (.) in the pattern matches all characters, including newlines. Without it, newlines are excluded. This modifier is equivalent to Perl's /s modifier. A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.
x
When this modifier is used, whitespace data characters in the pattern are ignored except when escaped or inside a character class, and characters between an unescaped # outside a character class and the next newline character, inclusive, are also ignored. This is equivalent to Perl's /x modifier, and makes it possible to include comments inside complicated patterns. Note, however, that this applies only to data characters. Whitespace characters cannot appear within special character sequences in a pattern, for example within the sequence (?(, which introduces a conditional subpattern.
e
When this modifier is used, preg_replace() does normal substitution of references in the replacement string, evaluates it as PHP code, and uses the result of the evaluation for replacing the match found by the pattern.
Only preg_replace() uses this modifier; it's ignored by other PCRE functions.
A
When this modifier is used, the pattern is forced to be "anchored"; that is, it's constrained to match only at the start of the string that's being searched (the "subject string"). This effect can also be achieved by appropriate constructs in the pattern itself, which is the only way to do it in Perl.
D
When this modifier is used, a dollar metacharacter ($) in the pattern matches only at the end of the subject string. Without this modifier, a dollar sign also matches immediately before the final character if it's a newline (but not before any other newlines). This modifier is ignored if the /m modifier is set. There is no equivalent to this modifier in Perl.
S
When a pattern is going to be used several times, it's worth spending more time analyzing it in order to speed up the time taken for matching. When this modifier is used, this extra analysis is performed. At present, studying a pattern is useful only for non-anchored patterns that don't have a single fixed starting character. This is equivalent to the study() function in Perl.
U
This modifier inverts the "greediness" of the quantifiers so that they're not greedy by default, but become greedy if followed by "?". Greedy quantifiers attempt to match as much of the target string as they legally can. The only limit on this behavior is that the greediness of one quantifier cannot cause the following other quantifiers in the pattern to fail. This modifier is not compatible with Perl.
X
This modifier turns on additional functionality of PCRE that is incompatible with Perl. Any backslash in a pattern that's followed by a letter that has no special meaning causes an error, thus reserving these combinations for future expansion. By default, as in Perl, a backslash followed by a letter with no special meaning is treated as a literal. At present, no other features are controlled by this modifier.
When this modifier is used, the matching of alphabetic characters in the pattern becomes non-case-sensitive; for example, "/sgi/i" matches both "sgi" and "SGI." This is equivalent to Perl's /i modifier.
m
By default, PCRE treats the subject string as consisting of a single "line" of characters (even if it actually contains several newlines). The "start of line" metacharacter (^) matches only at the start of the string, while the "end of line" metacharacter ($) matches only at the end of the string, or before a terminating newline (unless the D modifier is also set). This is the same as in Perl.
When this modifier is used, the "start of line" and "end of line" constructs match immediately following or immediately before any newline in the subject string, respectively, as well as at the very start and end. This is equivalent to Perl's /m modifier. If there are no "\n" characters in a subject string, or no occurrences of ^ or $ in a pattern, setting this modifier has no effect.
s
When this modifier is used, a dot metacharacter (.) in the pattern matches all characters, including newlines. Without it, newlines are excluded. This modifier is equivalent to Perl's /s modifier. A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.
x
When this modifier is used, whitespace data characters in the pattern are ignored except when escaped or inside a character class, and characters between an unescaped # outside a character class and the next newline character, inclusive, are also ignored. This is equivalent to Perl's /x modifier, and makes it possible to include comments inside complicated patterns. Note, however, that this applies only to data characters. Whitespace characters cannot appear within special character sequences in a pattern, for example within the sequence (?(, which introduces a conditional subpattern.
e
When this modifier is used, preg_replace() does normal substitution of references in the replacement string, evaluates it as PHP code, and uses the result of the evaluation for replacing the match found by the pattern.
Only preg_replace() uses this modifier; it's ignored by other PCRE functions.
A
When this modifier is used, the pattern is forced to be "anchored"; that is, it's constrained to match only at the start of the string that's being searched (the "subject string"). This effect can also be achieved by appropriate constructs in the pattern itself, which is the only way to do it in Perl.
D
When this modifier is used, a dollar metacharacter ($) in the pattern matches only at the end of the subject string. Without this modifier, a dollar sign also matches immediately before the final character if it's a newline (but not before any other newlines). This modifier is ignored if the /m modifier is set. There is no equivalent to this modifier in Perl.
S
When a pattern is going to be used several times, it's worth spending more time analyzing it in order to speed up the time taken for matching. When this modifier is used, this extra analysis is performed. At present, studying a pattern is useful only for non-anchored patterns that don't have a single fixed starting character. This is equivalent to the study() function in Perl.
U
This modifier inverts the "greediness" of the quantifiers so that they're not greedy by default, but become greedy if followed by "?". Greedy quantifiers attempt to match as much of the target string as they legally can. The only limit on this behavior is that the greediness of one quantifier cannot cause the following other quantifiers in the pattern to fail. This modifier is not compatible with Perl.
X
This modifier turns on additional functionality of PCRE that is incompatible with Perl. Any backslash in a pattern that's followed by a letter that has no special meaning causes an error, thus reserving these combinations for future expansion. By default, as in Perl, a backslash followed by a letter with no special meaning is treated as a literal. At present, no other features are controlled by this modifier.
Thursday, October 15, 2009
Email validation function
function validEmail($email) {
$email = trim($email);
$isValid = true;
$atIndex = strrpos($email, "@");
if (is_bool($atIndex) && !$atIndex)
{
$isValid = false;
}
else
{
$domain = substr($email, $atIndex+1);
$local = substr($email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64)
{
// local part length exceeded
$isValid = false;
}
else if (strpos($domain, ".") === false) {
//domain does not have a period
$isValid = false;
}
else if ($domainLen < 1 || $domainLen > 255)
{
// domain part length exceeded
$isValid = false;
}
else if ($local[0] == '.' || $local[$localLen-1] == '.')
{
// local part starts or ends with '.'
$isValid = false;
}
else if (preg_match('/\\.\\./', $local))
{
// local part has two consecutive dots
$isValid = false;
}
else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
{
// character not valid in domain part
$isValid = false;
}
else if (preg_match('/\\.\\./', $domain))
{
// domain part has two consecutive dots
$isValid = false;
}
else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',str_replace("\\\\","",$local)))
{
// character not valid in local part unless
// local part is quoted
if (!preg_match('/^"(\\\\"|[^"])+"$/',str_replace("\\\\","",$local)))
{
$isValid = false;
}
}
//remove, just check formatting for now
/*if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
{
// domain not found in DNS
$isValid = false;
}
*/
}
return $isValid;
}
$email = trim($email);
$isValid = true;
$atIndex = strrpos($email, "@");
if (is_bool($atIndex) && !$atIndex)
{
$isValid = false;
}
else
{
$domain = substr($email, $atIndex+1);
$local = substr($email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64)
{
// local part length exceeded
$isValid = false;
}
else if (strpos($domain, ".") === false) {
//domain does not have a period
$isValid = false;
}
else if ($domainLen < 1 || $domainLen > 255)
{
// domain part length exceeded
$isValid = false;
}
else if ($local[0] == '.' || $local[$localLen-1] == '.')
{
// local part starts or ends with '.'
$isValid = false;
}
else if (preg_match('/\\.\\./', $local))
{
// local part has two consecutive dots
$isValid = false;
}
else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
{
// character not valid in domain part
$isValid = false;
}
else if (preg_match('/\\.\\./', $domain))
{
// domain part has two consecutive dots
$isValid = false;
}
else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',str_replace("\\\\","",$local)))
{
// character not valid in local part unless
// local part is quoted
if (!preg_match('/^"(\\\\"|[^"])+"$/',str_replace("\\\\","",$local)))
{
$isValid = false;
}
}
//remove, just check formatting for now
/*if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
{
// domain not found in DNS
$isValid = false;
}
*/
}
return $isValid;
}
Create State Drop Down Menu
function makeStateDropDown($state='') {
$state = trim($state);
$dropdown = "";
$states = array(
"Alabama" => "AL",
"Alaska" => "AK",
"Arizona" => "AZ",
"Arkansas" => "AR",
"California" => "CA",
"Colorado" => "CO",
"Connecticut" => "CT",
"Delaware" => "DE",
"Florida" => "FL",
"Georgia" => "GA",
"Hawaii" => "HI",
"Idaho" => "ID",
"Illinois" => "IL",
"Indiana" => "IN",
"Iowa" => "IA",
"Kansas" => "KS",
"Kentucky" => "KY",
"Louisiana" => "LA",
"Maine" => "ME",
"Maryland" => "MD",
"Massachusetts" => "MA",
"Michigan" => "MI",
"Minnesota" => "MN",
"Mississippi" => "MS",
"Missouri" => "MO",
"Montana" => "MT",
"Nebraska" => "NE",
"Nevada" => "NV",
"New Hampshire" => "NH",
"New Jersey" => "NJ",
"New Mexico" => "NM",
"New York" => "NY",
"North Carolina" => "NC",
"North Dakota" => "ND",
"Ohio" => "OH",
"Oklahoma" => "OK",
"Oregon" => "OR",
"Pennsylvania" => "PA",
"Rhode Island" => "RI",
"South Carolina" => "SC",
"South Dakota" => "SD",
"Tennessee" => "TN",
"Texas" => "TX",
"Utah" => "UT",
"Vermont" => "VT",
"Virginia" => "VA",
"Washington" => "WA",
"Washington DC" => "DC",
"West Virginia" => "WV",
"Wisconsin" => "WI",
"Wyoming" => "WY"
);
foreach($states as $key=>$val) {
$selected = "";
if(strtolower($key) == strtolower($state) || strtolower($val) == strtolower($state)) {
$selected = " selected";
}
$dropdown = "$dropdown \n";
}
return $dropdown;
}
$state = trim($state);
$dropdown = "";
$states = array(
"Alabama" => "AL",
"Alaska" => "AK",
"Arizona" => "AZ",
"Arkansas" => "AR",
"California" => "CA",
"Colorado" => "CO",
"Connecticut" => "CT",
"Delaware" => "DE",
"Florida" => "FL",
"Georgia" => "GA",
"Hawaii" => "HI",
"Idaho" => "ID",
"Illinois" => "IL",
"Indiana" => "IN",
"Iowa" => "IA",
"Kansas" => "KS",
"Kentucky" => "KY",
"Louisiana" => "LA",
"Maine" => "ME",
"Maryland" => "MD",
"Massachusetts" => "MA",
"Michigan" => "MI",
"Minnesota" => "MN",
"Mississippi" => "MS",
"Missouri" => "MO",
"Montana" => "MT",
"Nebraska" => "NE",
"Nevada" => "NV",
"New Hampshire" => "NH",
"New Jersey" => "NJ",
"New Mexico" => "NM",
"New York" => "NY",
"North Carolina" => "NC",
"North Dakota" => "ND",
"Ohio" => "OH",
"Oklahoma" => "OK",
"Oregon" => "OR",
"Pennsylvania" => "PA",
"Rhode Island" => "RI",
"South Carolina" => "SC",
"South Dakota" => "SD",
"Tennessee" => "TN",
"Texas" => "TX",
"Utah" => "UT",
"Vermont" => "VT",
"Virginia" => "VA",
"Washington" => "WA",
"Washington DC" => "DC",
"West Virginia" => "WV",
"Wisconsin" => "WI",
"Wyoming" => "WY"
);
foreach($states as $key=>$val) {
$selected = "";
if(strtolower($key) == strtolower($state) || strtolower($val) == strtolower($state)) {
$selected = " selected";
}
$dropdown = "$dropdown \n";
}
return $dropdown;
}
Number to Roman
function numberToRoman($num)
{
// Make sure that we only use the integer portion of the value
$n = intval($num);
$result = '';
// Declare a lookup array that we will use to traverse the number:
$lookup = array('M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400,
'C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);
foreach ($lookup as $roman => $value)
{
// Determine the number of matches
$matches = intval($n / $value);
// Store that many characters
$result .= str_repeat($roman, $matches);
// Substract that from the number
$n = $n % $value;
}
// The Roman numeral should be built, return it
return $result;
}
{
// Make sure that we only use the integer portion of the value
$n = intval($num);
$result = '';
// Declare a lookup array that we will use to traverse the number:
$lookup = array('M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400,
'C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);
foreach ($lookup as $roman => $value)
{
// Determine the number of matches
$matches = intval($n / $value);
// Store that many characters
$result .= str_repeat($roman, $matches);
// Substract that from the number
$n = $n % $value;
}
// The Roman numeral should be built, return it
return $result;
}
Roman to number function
function romanToNumber($input)
{
$intvalues = array();
$intLoopI = 0;
$intTotal = 0;
$intPreviousPointer = 99999;
$intCurrentPointer = 0;
$intvalues['M'] = 1000;
$intvalues['D'] = 500;
$intvalues['C'] = 100;
$intvalues['L'] = 50;
$intvalues['X'] = 10;
$intvalues['V'] = 5;
$intvalues['I'] = 1;
for ($intLoopI = 0; $intLoopI < strlen($input) - 1; $intLoopI++)
{
$intCurrentPointer = $intvalues[substr($input, $intLoopI, 1)];
$intTotal += $intCurrentPointer;
if ($intCurrentPointer > $intPreviousPointer)
$intTotal -= (2 * $intPreviousPointer);
$intPreviousPointer = $intCurrentPointer;
}
return $intTotal;
}
{
$intvalues = array();
$intLoopI = 0;
$intTotal = 0;
$intPreviousPointer = 99999;
$intCurrentPointer = 0;
$intvalues['M'] = 1000;
$intvalues['D'] = 500;
$intvalues['C'] = 100;
$intvalues['L'] = 50;
$intvalues['X'] = 10;
$intvalues['V'] = 5;
$intvalues['I'] = 1;
for ($intLoopI = 0; $intLoopI < strlen($input) - 1; $intLoopI++)
{
$intCurrentPointer = $intvalues[substr($input, $intLoopI, 1)];
$intTotal += $intCurrentPointer;
if ($intCurrentPointer > $intPreviousPointer)
$intTotal -= (2 * $intPreviousPointer);
$intPreviousPointer = $intCurrentPointer;
}
return $intTotal;
}
implode on field
function implode_on_field($delimiter, Array $objects, $field)
{
$d = array();
foreach ($objects as $ob)
$d[] = $ob->{$field};
return implode($delimiter, $d);
}
======================================
example:
$obj = array();
for ($i = 0; $i < 5; $i++) {
$obj[] = array('id' => $i, 'type' => 'x');
}
$out = implode_on_field(',', $obj, 'id');
echo $out;
=======================================
output:
1,2,3,4,5
{
$d = array();
foreach ($objects as $ob)
$d[] = $ob->{$field};
return implode($delimiter, $d);
}
======================================
example:
$obj = array();
for ($i = 0; $i < 5; $i++) {
$obj[] = array('id' => $i, 'type' => 'x');
}
$out = implode_on_field(',', $obj, 'id');
echo $out;
=======================================
output:
1,2,3,4,5
Wednesday, October 14, 2009
Date query tips
Use 'between' when searching for rows based on dates instead of using 'like'.
Reason: 'like' doesnt search using indexes so it might slow down your query.
$today = date('Y-m-d');
$firstDayMonth = date("Y-m-d", mktime(0, 0, 0, date("m") , date("d")-date("d")+1, date("Y")));
examples:
-bad
(SELECT SUM(amount) from merchant_lead_order_charges WHERE merchant_lead_order_charges.date is between '" . date('Y-m') . "%')
-good
(SELECT SUM(amount) from merchant_lead_order_charges WHERE merchant_lead_order_charges.date between '" . $firstDayMonth . "' and '".$today."')
Reason: 'like' doesnt search using indexes so it might slow down your query.
$today = date('Y-m-d');
$firstDayMonth = date("Y-m-d", mktime(0, 0, 0, date("m") , date("d")-date("d")+1, date("Y")));
examples:
-bad
(SELECT SUM(amount) from merchant_lead_order_charges WHERE merchant_lead_order_charges.date is between '" . date('Y-m') . "%')
-good
(SELECT SUM(amount) from merchant_lead_order_charges WHERE merchant_lead_order_charges.date between '" . $firstDayMonth . "' and '".$today."')
Subscribe to:
Posts (Atom)