Monthly Archives: December 2010

PHP- Regular Expression

Regular Expression is really messy … complicated .. i used to think like that before….but it really is’nt . Because before i used to jump at once.I used to read a lot but not from the fresh start..So i get messed up with long expression.Then i made find to read it from the begining, the very fresh start..Then i …started learning.Ok here is the start.
Regular Expression is commonly called as Regex.when you look at a regular expression containing a sequence of special characters like /, $, ^, \, ?, *, etc., in combination with alphanumeric characters, you might think it a mess. RegEx is a kind of language and if you have learnt its symbols and understood their meaning, you would find it as the most useful tool in hand to solve many complex problems related to text searches.


Note: please first know about the meaning of these symbols/characters first

Explanation:
First of all, let’s take a look at two special symbols: ‘^’ and ‘$’. What they do is indicate the
start and the end of a string, respectively, like this:

“^he”: matches any string that starts with “he”;
“is a boy$”: matches a string that ends in the substring “is a boy”;
“^abc$”: a string that starts and ends with “abc” — that could only be “abc” itself!
“test”: a string that has the text “test” in it.
You can see that if you don’t use either of the two characters we mentioned, as in the last example,
you’re saying that the pattern may occur anywhere inside the string — you’re not “hooking” it to any of the edges.

There are also the symbols ‘*’, ‘+’, and ‘?’, which denote the number of times a character or a sequence of
characters may occur. What they mean is: “zero or more”, “one or more”, and “zero or one.” Here are some examples:

"ab*": matches a string that has an a followed by zero or more b's ("a", "ab", "abbb", etc.);
"ab+": same, but there's at least one b ("ab", "abbb", etc.);
"ab?": there might be a b or not;
"a?b+$": a possible a followed by one or more b's ending a string.

You can also use bounds, which come inside braces and indicate ranges in the number of occurences:

"ab{2}": matches a string that has an a followed by exactly two b's ("abb");
"ab{2,}": there are at least two b's ("abb", "abbbb", etc.);
"ab{3,5}": from three to five b's ("abbb", "abbbb", or "abbbbb").

Note that you must always specify the first number of a range (i.e, “{0,2}”, not “{,2}”). Also, as you might
have noticed, the symbols ‘*’, ‘+’, and ‘?’ have the same effect as using the bounds “{0,}”, “{1,}”, and “{0,1}”,
respectively.
There are also the symbols ‘*’, ‘+’, and ‘?’, which denote the number of times a character or a sequence of
characters may occur. What they mean is: “zero or more”, “one or more”, and “zero or one.” Here are some examples:

"ab*": matches a string that has an a followed by zero or more b's ("a", "ab", "abbb", etc.);
"ab+": same, but there's at least one b ("ab", "abbb", etc.);
"ab?": there might be a b or not;
"a?b+$": a possible a followed by one or more b's ending a string.

You can also use bounds, which come inside braces and indicate ranges in the number of occurences:

"ab{2}": matches a string that has an a followed by exactly two b's ("abb");
"ab{2,}": there are at least two b's ("abb", "abbbb", etc.);
"ab{3,5}": from three to five b's ("abbb", "abbbb", or "abbbbb").

Note that you must always specify the first number of a range (i.e, “{0,2}”, not “{,2}”). Also, as you might
have noticed, the symbols ‘*’, ‘+’, and ‘?’ have the same effect as using the bounds “{0,}”, “{1,}”, and “{0,1}”,
respectively.

Now, to quantify a sequence of characters, put them inside parentheses:

"a(bc)*": matches a string that has an a followed by zero or more copies of the sequence "bc";
"a(bc){1,5}": one through five copies of "bc."
There's also the '|' symbol, which works as an OR operator:

"hi|hello": matches a string that has either "hi" or "hello" in it;
"(b|cd)ef": a string that has either "bef" or "cdef";
"(a|b)*c": a string that has a sequence of alternating a's and b's ending in a c;
A period ('.') stands for any single character:

"a.[0-9]": matches a string that has an a followed by one character and a digit;
"^.{3}$": a string with exactly 3 characters.

Bracket expressions specify which characters are allowed in a single position of a string:

"[ab]": matches a string that has either an a or a b (that's the same as "a|b");
"[a-d]": a string that has lowercase letters 'a' through 'd' (that's equal to "a|b|c|d" and even "[abcd]");
"^[a-zA-Z]": a string that starts with a letter;
"[0-9]%": a string that has a single digit before a percent sign;
",[a-zA-Z0-9]$": a string that ends in a comma followed by an alphanumeric character.

You can also list which characters you DON’T want — just use a ‘^’ as the first symbol in a bracket expression
(i.e., “%[^a-zA-Z]%” matches a string with a character that is not a letter between two percent signs).

In order to be taken literally, you must escape the characters “^.[$()|*+?{\” with a backslash (‘\’), as
they have special meaning. On top of that, you must escape the backslash character itself in PHP3 strings, so,
for instance, the regular expression “(\$|Â¥)[0-9]+” would have the function call: ereg(“(\\$|Â¥)[0-9]+”, $str)
Extracted From:http://weblogtoolscollection.com/

Similar Links

LINK ONE
LINK TWO

LINK THREE

Regular Expression sites

Regexlib

Regular Expression

Microsoft Regex

PHP- String check for numeric and alphabet

This tutorial explains about the PHP function ctype_alpha,ctype_alnum(),is_numeric().

ctype_alpha():

This function returns true(1) if the tested string is alphabet.y using ctype_alpha function in php. We can check or validate alphabetic characters only. Using this function we can check any input coming from insecure ( or any other ) sources. This function is case insensitive so can’t differentiate between lower case and upper case alphabetic characters.

$var=”abctYB”;

if(ctype_alpha($var)){
echo ” This is alphabetic “;
}else{ echo “this is not alphabetic”;}

Also check for
  • ctype_upper() – Check for uppercase character(s)
  • ctype_lower() – Check for lowercase character(s)
  • ctype_alnum()- Check for alpha numeric

PHP- How to send E-mail Using PHP mail() function

This tutorial explains about sending email through php mail() function. Before we studied the simple mailing system. Now , this tutorial explains about how to send the email with html format . First, lets introduced with the term

MIME:

Multipurpose Internet Mail Extensions (MIME) is an Internet standard that extends the format of e-mail to support.MIME defines mechanisms for sending other kinds of information in e-mail

Here is the code sample

<?php
$to = “receiver1@example.com, receiver@gmail.com”;
$subject = “subject to be sent”;

$message = ”
<html>
<head>
<title>Email including html …</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Roman</td>
<td>BIkesh</td>
</tr>
</table>
</body>
</html>
“;

// Always set content-type when sending HTML email
$headers = “MIME-Version: 1.0” . “\r\n”;
$headers .= “Content-type:text/html;charset=iso-8859-1” . “\r\n”;

// More headers
$headers .= ‘From: <krishna@example.com>’ . “\r\n”;
$headers .= ‘Cc: abc@example.com’ . “\r\n”;

mail($to,$subject,$message,$headers);
?>

Sending Email with Attachment


–PHP-mixed-
Content-Type: multipart/alternative; boundary=”PHP-alt-

–PHP-alt-
Content-Type: text/plain; charset=”iso-8859-1″
Content-Transfer-Encoding: 7bit

Hello World!!!
This is simple text email message.

–PHP-alt-
Content-Type: text/html; charset=”iso-8859-1″
Content-Transfer-Encoding: 7bit

Hello World!

This is something with HTML formatting.

–PHP-alt-

–PHP-mixed-
Content-Type: application/zip; name=”attachment.zip”
Content-Transfer-Encoding: base64
Content-Disposition: attachment


–PHP-mixed-

Similar Links:
Link 1
Link 2
Link 3

Javascript- Auto refresh page

This tutorial explains how to refresh the page in a certain interval. Sometime you need to refresh the page automatically while using the session. You need to load the latest content automatically . So the webpage need to be refreshed.

Sample code
<script type=”text/JavaScript”>

function autoRefresh(timeToRefresh) {
setTimeout(“location.reload(true);”,timeToRefresh);
}

</script>
<p><a href=”javascript:autoRefresh(1000)”>Refresh page in 1 sec</a></p>

Next Example:

function updateDivContent(){
    // Assuming we have #shoutbox
    $('#divtoreload').load('page.php');
}
setInterval( "updateDivContent()", 10000 );

Business services and Business news Business international onlinlen publishing Business

Get the width and height of an image using a function in PHP

PHP provides a function getimagesize(). This function helps to find the image height,widht, type and attribute.

Here is the sample code:

&lt;?php

if(isset($_POST['submit'])){  // code executes on submit event

list($width, $height, $typeOfFile, $attribute) = getimagesize('$_FILES['imagename']['tmp_name']');
 //&lt;input type='file' name='imagename'

echo $width;  //output:300

echo $height; //output:400

echo $typeOfFile; //Output:2

echo $attribute;  //output: <strong>width="300" height="400"</strong>

}

?&gt;

NOTE:

Type of the image
1 = GIF 5 = PSD 9 = JPC 13 = SWC
2 = JPG 6 = BMP 10 = JP2 14 = IFF
3 = PNG 7 = TIFF(intel byte order) 11 = JPX 15 = WBMP
4 = SWF 8 = TIFF(motorola byte order) 12 = JB2 16 = XBM