Wednesday, October 10, 2012
Friday, September 28, 2012
Regular expression for finding the special chars present in a given string
<?php
$str = "qdsdasdadsasd\";
$exp ="~@#$%^&*()_+=-[]'\|; \" :,./?";
if (preg_match("/[\~@#$%^&*()_+=-\[\]\'|;,.?\/\\\\\\\\]/", $str)) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
?>
$str = "qdsdasdadsasd\";
$exp ="~@#$%^&*()_+=-[]'\|; \" :,./?";
if (preg_match("/[\~@#$%^&*()_+=-\[\]\'|;,.?\/\\\\\\\\]/", $str)) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
?>
Thursday, September 20, 2012
Regular expression for allow only numbers and symbols along with one number should be present
var str = "3452.345-345";
var format = /^(?=.*\d)[0-9\s\.\-\_\(\)\+]+$/;
if(!format.test(str)){
alert("Invalid input")
} else {
alert("Valid input")
}
Details
(?=.*\d) :- at least one digit should be present
[0-9\s\.\-\_\(\)\+] - given input should be 0 to 9 or symbols like (-_ . space +)
var format = /^(?=.*\d)[0-9\s\.\-\_\(\)\+]+$/;
if(!format.test(str)){
alert("Invalid input")
} else {
alert("Valid input")
}
Details
(?=.*\d) :- at least one digit should be present
[0-9\s\.\-\_\(\)\+] - given input should be 0 to 9 or symbols like (-_ . space +)
Thursday, September 13, 2012
Regular expression for (Filtering the URLS from a given string)
<?php
// The Regular Expression filter for urls (http://www.sometext.com)
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$text = "The text you want to filter goes here. http://www.google.com";
if(preg_match($reg_exUrl, $text, $url)) {
echo $url[0];
} else {
echo "No patterns matched"
}
// The Regular Expression filter for urls (www.sometext.com)
$reg_exUrl = "/(www|WWW|)\.[a-zA-Z0-9\-\.]+\.(com|in|)?/";
$text = 'some texts http://www.yahoo.in/bin/set/?ilc=37 some texts';
if(preg_match($reg_exUrl, $text, $url)) {
echo $url[0];
} else {
echo "No patterns matched"
}
?>
// The Regular Expression filter for urls (http://www.sometext.com)
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$text = "The text you want to filter goes here. http://www.google.com";
if(preg_match($reg_exUrl, $text, $url)) {
echo $url[0];
} else {
echo "No patterns matched"
}
// The Regular Expression filter for urls (www.sometext.com)
$reg_exUrl = "/(www|WWW|)\.[a-zA-Z0-9\-\.]+\.(com|in|)?/";
$text = 'some texts http://www.yahoo.in/bin/set/?ilc=37 some texts';
if(preg_match($reg_exUrl, $text, $url)) {
echo $url[0];
} else {
echo "No patterns matched"
}
?>
Tuesday, September 4, 2012
Running PHP programs in windows command prompt
C:\Users>d:
D:\>cd xampp\php
D:\xampp\php>php d:\xampp\htdocs\samy\test1.php
D:\>cd xampp\php
D:\xampp\php>php d:\xampp\htdocs\samy\test1.php
Tuesday, August 28, 2012
Friday, July 6, 2012
Replacing nth matching string using php
<?php
//Replacing nth matching string
$string = 'mugesh php programmer java programmer php java programmer php tester php is open source.';
$replaceme = 'php';
$replacewith = 'PHP5';
$nthtimes = 4;
echo preg_replace("/((.*?)(".$replaceme.")){".$nthtimes."}/e", '(preg_replace("/".$replaceme."$/", "", "\0")).$replacewith', $string);
$result = preg_split("/({$replaceme})/",$string,$nthtimes,PREG_SPLIT_DELIM_CAPTURE);
array_push($result,preg_replace("/{$replaceme}/",$replacewith,array_pop($result),1));
echo implode($result);
?>
//Replacing nth matching string
$string = 'mugesh php programmer java programmer php java programmer php tester php is open source.';
$replaceme = 'php';
$replacewith = 'PHP5';
$nthtimes = 4;
echo preg_replace("/((.*?)(".$replaceme.")){".$nthtimes."}/e", '(preg_replace("/".$replaceme."$/", "", "\0")).$replacewith', $string);
$result = preg_split("/({$replaceme})/",$string,$nthtimes,PREG_SPLIT_DELIM_CAPTURE);
array_push($result,preg_replace("/{$replaceme}/",$replacewith,array_pop($result),1));
echo implode($result);
?>
Subscribe to:
Posts (Atom)