Pages

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.";
}

?>

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 +)

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"
}
?>

Tuesday, September 4, 2012