Get Major Training|Project Training|Live Project|Industrial Training On Android| I-Phone | Java | J2SE | J2EE | C#.Net | ASP.Net| PHP | PHP with Wordpress | Joomla | Majento Contact us

Monday

Basic PHP useful functions inbuilt

Hi guys, hope u are doing well with your works today we are discussing about some useful function which are inbuilt in php and can become handy in developing web application

Lets talks some funtions now

String Function in PHP

1)strlen(string str)
<code>
<?php $pswd="PHPwebsite";
if(strlen($pswd)<12)
{echo "Password is too small";
}
else
{echo "password length Ok";
}

?> <\code>

2)strcmp(string str1,string str2)
it returns
0 when both str1 and str2 will equals
-1 when str1 is less than str2
1 when str1 is greater than str2
<code>
<?php
str1="PHP";
str2="guru";
if(strcmp(str1,str2)!=0)
{echo "String not matched";
}
?>
<\code>
3)strcasecmp (str1,str2)
it compare string without case sensetivity <code>
<?php $mail="info@vinayakinformatics.com";
$mail1="INFO@VINAYAKINFORMATICS.COM";
if(!strcasecmp($mail,$mail1))//check condition not true
{echo "email not matched";
}
else
{echo "Matched";
}
?> <\code>

4)strspn(str1,str2)
these funtion matches the string length of string1 with string 2
character found in str1 <code>
<?php
$password="123454";
if(strspn($password,"1234567890")==strlen($password))
{echo "The password don't contains number only please add character also";
}
?> <\code>

5)strcspn(str1,str2)
these funtion is resvers of above funtion
it returns lenght of character not found in str2 <code>
<?php
$password="123454";
if(strcspn($password,"1234567890")==0)
{echo "The password don't contains number only please add character also";
}
?>//these code dont show echo msg <\code>

6)strtolower(str)
these convert all string contain to lower case <code>
<?php $name="PHP TRAINING";
echo strtolower($name);
//these will echo
//php training
?>
<\code>
7)strtoupper(str)
these convert all string contain to upper case <code>
<?php $name="best live training ";
echo strtolower($name);
//these will echo
//BEST LIVE TRAINING
?> <\code>

8)ucfirst(str)
these funtion capatilize first character of a string
and no non alphabetical character affected <code>
<?php
$srt="vinayak informatics is best php development and training company";
echo ucfirst($str);
?> <\code>
output
Vinayak informatics is best php development and training company

9)ucwords(str)
these function capatilize each letter of each word in string
<code> <?php
$str="learn php from professional developers with live support and friendly enviorment at vinayak informatics";
echo ucword($str);
?>
output
Learn Php From Professional Developers With Live Support And Friendly Enviorment At Vinayak Informatics
<\code>
10)htmlspecialchars(str)
these function is used for converting character to there displayable equvivalent
like
& becomes &
" become &quot
' become '
< becomes <<
> becomes >>

these function generally used to avoid sql injection on url or input type <code>
<?php $input="I just can't read <enough> of php";
?>
output:I just can&#039t; read <<enough>> of PHP <\code>

11)strtr($str,array $rep)
these convert all character in str to their corresponding match in
rep array <code>
<?php
$rep=array("<b>"=>"<strong>","</b>"=>"</strong>");
$str="<b>Vinayak informatic offers Website in just $100</b>";
echo strtr($str,$rep);
?>

output:<strong>Vinayak informatic offers Website in just $100</strong> <\code>

12)strip_tags($str)
these convert html text/file to plain text/file
<?php
$str="mail us<a href='info@vinayakinformatics.com'>vinayak official id</a>";
echo strip_tags($str);
?>

output:mail us info@vinayakinformatics.com
<\code>
13)explode(string seperator,string str)
these function split the str into array with given seperator <code>
<?php
$str="mailus at info@vinayakinformatics.com";
echo(explode('@',$str);
?>
output
vinayakinformatics.com
<\code>
14)implode(string delimiter,array str)
these function joins the str from array with given seperator into string <code>
<?php
$str=array("Website","training","php","indore","domain name","hosting");
echo(implode('|',$str);
?>
output:website|training|php|indore|domain name|hosting <\code>

15)strpos(str,string to match)
these function find first occurence of string to match in str <code>
<?php
$str="PHP BEST TRAINING AT INDORE"
echo strpos($str,'T');
?>
output:8
<\code>
16)str_replace(str to replace,replacement,string)
it replace str to replace with replacement occerd in string given <code>
<?php
$author="vikas@vinayakinformatics.com";
$author=str_replace("@","at",$author);
echo $author;
?>
output:vikasatvinayakinformatics.com <\code>

17)sunstr_replace(str,replacement,start lenght,total lenght)
these replace str contain with replacement from starting lenght to total
given lenght <code>
<?php
$mobile="9993253336";
echo substr_replace($mobile,"",1,8);
?>
output=96
<\code>
18)ltrim(str)
these revome space,\t,\n,\r,NULL,Vertical(\xob)from string from left to right

19)rtrim(str)
these revome space,\t,\n,\r,NULL,Vertical(\xob)from string from right to left

20)trim(str)
these revome space,\t,\n,\r,NULL,Vertical(\xob)from string from both side

21)str_pad(str)
these include space in string from desired word to defined lenght <code>
<?php
echo str_pad("PHP",10)."is best open source";
?>
output:PHP is best open source
<\code>
22)count_chars(str)
these count characters in string
<code> <?php
$str="get your website in just $100*";
echo count_chars($str);
?>
output:30
<\code>
23)str_word_count(str)
these count characters in string <code>
<?php
$str="get your website in just $100*";
echo str_word_count($str);
?>
output:6
<\code>
Bonus : what if you need to take difference of time taken by two athletes in a race how can you find it
let me show you how it will be done

The code bellow will subtract and
get the difference of two time using
the php date and short time
function. Copy thye code bellow and
paste to your php editor and save it
as "index.php". <code>
1. <?php
2. $firsttime = strtotime( '02:54:17' ) ;
3. $secondtime = strtotime ( '01:54:17' );
4. $result = $firsttime - $secondtime ;
5. echo 'First Time : ' . date ( 'H:i:s' , $firsttime ) . '<br>' ;
6. echo 'Second Time : ' . date ('H:i:s' , $secondtime ). '<br>' ;
7. echo 'Result : ' . date ( 'H:i:s' , $result ) ;
8. ?> <\code>
Hope this code will help you in your
project.
Author: Vikas Agrawal
Vikas Agrawal is a CEO and Founder Of Vinayak Informatics Systems. Read More →

You Like It, Please Share This Article Using...



No comments:

Post a Comment

Please leave your comments and we will be reply you back ASAP
vikas agrawal