Do you see in some website a nice list of online users ? Do you want to have one in your website ? That should be easy now thanks to this very helpful and easy-to-install widget:
I hope you will like it :)
String Capitalization Functions: strtoupper-strtolower-ucwords.
In php you can easily manipulate the capitalization of your PHP strings, there are ready to use functions that help you to convert yout text to upper case, lower case, or just the first letter of every word to upper case.
This does explain everything about how to manipulate capitalization. In fact strtoupper returns the string in upper case, strtolower converts to lower case, and ucwords capitalize the first letter of each word, without making any changes to other characters in the word. (ie tEsT becomes TEsT with ucwords).
If you have any question, I'm ready to help you.
<?php
$originalString = "Testing string Capitalization AbCDe";
$upperCase = strtoupper($originalString); // Result: TESTING STRING CAPITALIZATION ABCDE
$lowerCase = strtolower($originalString); // Result: testing string capitalization abcde
$ucTitleString = ucwords($originalString); // Result: Testing String Capitalization Abcde
?>
$originalString = "Testing string Capitalization AbCDe";
$upperCase = strtoupper($originalString); // Result: TESTING STRING CAPITALIZATION ABCDE
$lowerCase = strtolower($originalString); // Result: testing string capitalization abcde
$ucTitleString = ucwords($originalString); // Result: Testing String Capitalization Abcde
?>
This does explain everything about how to manipulate capitalization. In fact strtoupper returns the string in upper case, strtolower converts to lower case, and ucwords capitalize the first letter of each word, without making any changes to other characters in the word. (ie tEsT becomes TEsT with ucwords).
If you have any question, I'm ready to help you.
PHP For Loop
When you need to do the same script many times, for exemple you want to send emails to all your users, you don't need to write a php code for each user, you just need to use for Loop which will do it for each one.
This is how:
Where send_email() is your own function that send the email to the user with the id as parameter.
Another exemple to For Loop, let's say you want to count 1+2x2+3x3+...+nxn, this is how you can do it:
So how For loop works ?
1. Set a counter variable to some initial value (i.e $i=0).
2. Check to see if the conditional statement is true(i.e $i<=10).
3. Execute the code within the loop.
4. Increment a counter at the end of each iteration through the loop(i.e $i++, or $i+=1, ou $i=$i+1) .
And this is how to do it:
The php loop will help you a lot in php programming, and there are other ways to do loops other that For, like while.
This is how:
<?php
for ($userid=1;$userid<=10;$userid++){
send_email($userid);
}
?>
for ($userid=1;$userid<=10;$userid++){
send_email($userid);
}
?>
Where send_email() is your own function that send the email to the user with the id as parameter.
Another exemple to For Loop, let's say you want to count 1+2x2+3x3+...+nxn, this is how you can do it:
<?php
$n=10;
$t=0;
for ($i=1;$i<=$n;$i++){
$t+=$i*$i; // the same as $t=$t+$i*$i
}
?>
$n=10;
$t=0;
for ($i=1;$i<=$n;$i++){
$t+=$i*$i; // the same as $t=$t+$i*$i
}
?>
So how For loop works ?
1. Set a counter variable to some initial value (i.e $i=0).
2. Check to see if the conditional statement is true(i.e $i<=10).
3. Execute the code within the loop.
4. Increment a counter at the end of each iteration through the loop(i.e $i++, or $i+=1, ou $i=$i+1) .
And this is how to do it:
for ( initialize a counter; conditional statement; increment a counter){
do this code;
}
do this code;
}
The php loop will help you a lot in php programming, and there are other ways to do loops other that For, like while.
PHP Validate Email Address: Email Validator
When working with forms and interacting with your visitors, you may want to verify if your visitor entered an email address in the right field, and if it is valide or not.
Most of people who are new to php world, just verify if the text box of the email address is filled or not, without verifying the syntax of the email address...
Something like this they use:
But when you want to develop a php website in the best way, taking care of all little details, you should think what happens when your user does make a mistake in the email address, writing something like this: his_email@gmailcom, or his_emailgmail.com or even he writes ssdfsff (like many people do), I'm sure you would like to inform him and ask him to correct his email address, you maybe also want to check if the domain name of his email address is valid, you can't accept an email like this: blahblah@dfsjhbhjjqsdqq.fdf .
So here is the php script I think you should use everytime you have a form with 'email address' input :
Now you can use this function with the email address you want to verify as parameter, and it will return you true if it valide and false if it is not.
This nice function will not know if an email exist really or not, it just verifies the syntax and the domain name, for example will not know that qsddsfqsdsdfqsdsqdsqd@gmail.com is not valide, but knows that qsqsdsq@dfsfsqdqsdqd.com is an invalid email address .
I hope this help you a lot.
Please leave comments or link to us if you think this is usefull :)
Most of people who are new to php world, just verify if the text box of the email address is filled or not, without verifying the syntax of the email address...
Something like this they use:
But when you want to develop a php website in the best way, taking care of all little details, you should think what happens when your user does make a mistake in the email address, writing something like this: his_email@gmailcom, or his_emailgmail.com or even he writes ssdfsff (like many people do), I'm sure you would like to inform him and ask him to correct his email address, you maybe also want to check if the domain name of his email address is valid, you can't accept an email like this: blahblah@dfsjhbhjjqsdqq.fdf .
So here is the php script I think you should use everytime you have a form with 'email address' input :
<?php
function validate_email($email) {
//check for all the non-printable codes in the standard ASCII set,
//including null bytes and newlines, and exit immediately if any are found.
if (preg_match("/[\\000-\\037]/",$email)) {
return false;
}
$pattern = "/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD";
if(!preg_match($pattern, $email)){
return false;
}
// Validate the domain exists with a DNS check
// if the checks cannot be made (soft fail over to true)
list($user,$domain) = explode('@',$email);
if( function_exists('checkdnsrr') ) {
if( !checkdnsrr($domain,"MX") ) { // Linux: PHP 4.3.0 and higher & Windows: PHP 5.3.0 and higher
return false;
}
}
else if( function_exists("getmxrr") ) {
if ( !getmxrr($domain, $mxhosts) ) {
return false;
}
}
return true;
} // end function validate_email
?>
function validate_email($email) {
//check for all the non-printable codes in the standard ASCII set,
//including null bytes and newlines, and exit immediately if any are found.
if (preg_match("/[\\000-\\037]/",$email)) {
return false;
}
$pattern = "/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD";
if(!preg_match($pattern, $email)){
return false;
}
// Validate the domain exists with a DNS check
// if the checks cannot be made (soft fail over to true)
list($user,$domain) = explode('@',$email);
if( function_exists('checkdnsrr') ) {
if( !checkdnsrr($domain,"MX") ) { // Linux: PHP 4.3.0 and higher & Windows: PHP 5.3.0 and higher
return false;
}
}
else if( function_exists("getmxrr") ) {
if ( !getmxrr($domain, $mxhosts) ) {
return false;
}
}
return true;
} // end function validate_email
?>
Now you can use this function with the email address you want to verify as parameter, and it will return you true if it valide and false if it is not.
This nice function will not know if an email exist really or not, it just verifies the syntax and the domain name, for example will not know that qsddsfqsdsdfqsdsqdsqd@gmail.com is not valide, but knows that qsqsdsq@dfsfsqdqsdqd.com is an invalid email address .
I hope this help you a lot.
Please leave comments or link to us if you think this is usefull :)
PHP Random code Generator
This is a very simple-to-use php function that help you generate random codes in very easy way, you will need this function to generate random passwords, confirmation links,...
So, this is it:
That's it!
There are many options you can play with, for example you can choose the characters to generate code from: $chars ="1234567890" will generate numeric code; $chars = "abcdefghijkmnopqrstuvwxyz023456789"; will generate alphanumeric codes. In fact the code will be generated randomly from the list of characters in $chars.
You can also change the lenght of the generated code by changing the value in "while ($i <= 9)", 9 here means you will get a code with 10 characters, as this counter (while) starts from 0 to 9. If you do "while ($i <= 7)" you will get 8 characters code.
This is the easiest and the best way to generate random code from php. I will always provide the best PHP scrips :)
If you like my blog, please give a link to us from your website, I will appreciate that :)
So, this is it:
<?php
function createRandomPassword() {
$chars = "abcdefghijkmnopqrstuvwxyz023456789";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i <= 9) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
?>
function createRandomPassword() {
$chars = "abcdefghijkmnopqrstuvwxyz023456789";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i <= 9) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
?>
That's it!
There are many options you can play with, for example you can choose the characters to generate code from: $chars ="1234567890" will generate numeric code; $chars = "abcdefghijkmnopqrstuvwxyz023456789"; will generate alphanumeric codes. In fact the code will be generated randomly from the list of characters in $chars.
You can also change the lenght of the generated code by changing the value in "while ($i <= 9)", 9 here means you will get a code with 10 characters, as this counter (while) starts from 0 to 9. If you do "while ($i <= 7)" you will get 8 characters code.
This is the easiest and the best way to generate random code from php. I will always provide the best PHP scrips :)
If you like my blog, please give a link to us from your website, I will appreciate that :)
Subscribe to:
Posts (Atom)
Search With Google
Categories
- 1-General (3)
- 2-Getting started (4)
- 3-Strings (10)
- 4-Server config (1)
- 5-Best scripts (7)
- email (4)
- files (6)
- Forms (3)
- If statements (3)
- Loop statements (2)
- Mysql (2)
- Server variables (3)
- Sessions (1)
- Tutorials-planet.com (1)
- web hosting (1)
- You need a programmer ? (1)