Tuesday 3 May 2011

Complete string functions in php





str_repeat: This function is used to repeat a string in specific number of times.

str_repeat script:
<?php
$str="welcome to dan php site";
echo str_repeat($str,2);
?>
str_replace: To replace the occurrence of the search string with replacement string.

str_replace Script
<?php
$str="welcome to dan php site";
echo str_replace("php","Java",$str);
?>
str_split: Converts a string as array. Same as implode.

str_word_count: This function returns the information about the words used in a string.

*If the mode is 0 it returns the total number of words. If the mode is 1 it returns the all words as array elements.

Each element value is one woed and element keys are 0 1 2 3 etc.

If the mode is 2 , the element keys are the index number of the words.

str_word_count script

<?php
$str="welcome to dan php site";
print_r(str_word_count(str,2));
?>
strstr: This function is used to return the characters from a string from specified position. It is case sensitive.

strstr script
<?php
$str="welcome to dan php site";
echo strstr($str,'p');
?>
stristr: It is same as “strstr”, but it is case insensitive.

strchr : It will find out the last occurred characters from a string , based on the input characters. And it returns the remaining string from that location.

strcmp: It compares two string values. It calculates the acsii value.

*It returns 0, if the both strings are same. If first string is greater than second string it displays the positive value. Otherwise it displays the negative value.

strchr script

<?php
$str="welcome to dan php site";
$str1="Welcome to dan php site";
echo strcmp($str,$str1);
?>

strcasechp: It is same as strcmp, But it is case insensitive.

strip_tags: By using this function we can strip the HTML tags from a string.

strip_tags script
<?php
$str="hello <input type='button' value ='click'>";
echo strip_tags($str);
?>

strpos: By using this function we can get the position of a character in a string.

stripos: It is same as strops. But it is case insensitive.

stripos script
<?php
$str="hello";
echo strpos($str,'e');
?>

strtolower: It converts the all characters into lower case characters.

strtoupper: It converts the all characters to uppercase characters.

substr: It returns the sub string of a string.

substr script:

<?php
$str="Welcome To My Site";
echo substr($str,2,3);
?>

string functions in php has been completed today. Tomorrow we are going to discourse an interesting topic. C u tomorrow guys . b bye

2 comments:

  1. sir, i want jquery libraries please upload in this www.danphp.com

    ReplyDelete
  2. Legend border line is missing partially in IE7.. can u let me know how to solve...

    ReplyDelete