Well, regular expressions have been something that I was scared of when I started coding as a serious stuff. The string literals puzzled me like anything. preg_match, preg_grep, preg_split, preg_replace etc have been something I always wanted to avoid. But thanks to phpbuilder and php that I can now handle them with some ease. And this is for you guys who find regular expressions tacky.
VERY BASICS:
1. "^tech" : Searches a string that starts with tech.
2. "logy$" : Searches for strings that ends up in logy.
3. "a*b" : Looks for a string that has either zero or more a’s but exactly one b following a. (eg. b, ab, aab, aaab, aaaaaaaaaaaaaaaaab etc.).
4. "a+b" : Same as a*b but only difference is that atleast one a should be there in the string unlike a*b which can overlook a. (eg. ab, aab,aaaaaaaaab etc).
Showing posts with label Regular Expression. Show all posts
Showing posts with label Regular Expression. Show all posts
Wednesday, March 10, 2010
Friday, January 29, 2010
Date and Time validation using Regular Expression
This function is used for date validation on a string
Return of this function is boolean.
This function is used for time validation on a string
Return of this function is boolean.
And This function is used for date_time validation on a string
Return of this function is boolean.
Return of this function is boolean.
function is_date($str){
$return = (bool) preg_match("(\b([1-9]([0-9]{3}))\-(1[0-2]|0[1-9])\-(3[01]|[12][0-9]|0[1-9])\b)",$str);
if(strlen($str)==10){
return $this->is_date_time($str);
}
if($return){
$return = (bool) (strlen($str)==10);
}
return $return;
}This function is used for time validation on a string
Return of this function is boolean.
function is_time($str){
$return = (bool) preg_match("(\b(2[0-3]|[01][0-9])\:([0-5][0-9])\:([0-5][0-9])\b)",$str);
if($return){
$return = (bool) (strlen($str)==8);
}
return $return;
}And This function is used for date_time validation on a string
Return of this function is boolean.
function is_date_time($str){
$return = (bool) preg_match("(\b([1-9]([0-9]{3}))\-(1[0-2]|0[1-9])\-(3[01]|[12][0-9]|0[1-9]) (2[0-3]|[01][0-9])\:([0-5][0-9])\:([0-5][0-9])\b)",$str);
if($return){
$return = (bool) (strlen($str)==19);
}
return $return;
}
Friday, January 15, 2010
URI Validation Using Regular Expression
who know about regular expression?
i need to validate a uri.
so create a validation script using preg_match() on PHP.
but, i get something wrong with my script. can you help me?
this is my uri validation function:
i need to validate a uri.
so create a validation script using preg_match() on PHP.
but, i get something wrong with my script. can you help me?
this is my uri validation function:
function valid_uri($str){
$patern = "/^((ht|f)tp(s?)\:\/\/|)([a-z0-9]([-.]*[a-z0-9])*(:(0-9)*)*([\/][a-zA-Z0-9\-\.\?\,\/\\\+&%\$#_=]*))?+$/i";
return preg_match($patern,$str);
}
and I have been tested:
$tes=array( "http://www.amikom.ac.id", "amikom.ac.id", "amikom.ac.id.", "ihi amikom.ac.id aha", "https://email.amikom.ac.id","www..com", "http://e-learning.com/", "e-learning.com/index.php", "e-learning.com/index.php?x=1234&a=897", "bukan uri","anang.",".anang.",".com", "http://.com","saya.#@%\$#ini.com");
foreach($tes as $item){
echo "\n".(($this->validasi->valid_uri($item))?"yes":"no")." ".$item;
}
and this is the result:
yes http://www.amikom.ac.id
no amikom.ac.id
no amikom.ac.id.
no ihi amikom.ac.id aha
yes https://email.amikom.ac.id
no www..com
yes http://e-learning.com/
yes e-learning.com/index.php
yes e-learning.com/index.php?x=1234&a=897
no bukan uri
no anang.
no .anang.
no .com
yes http://.com
no saya.#@%$#ini.com
".com" is not valid url, but why "http://.com" is valid?
i just stuck here.
anyone can help me?
Subscribe to:
Posts (Atom)
