PHP

PHP String handling functions

we have a lot of php predefined constants for string handling. lets have a look on few of them

  1. strlen()
    this function was used to find the length of the string.
    Syntax: strlen(variable name)
  2. str_replace()
    used to replace a part of string with another string.
    Syntax: str_replace(search,replace,[number of replaces])
  3. str_ireplace()
    It was same as str_replace but it was case sensitive.
    Syntax: str_replace(search,replace,[number of replaces])
  4. trim()
    it was used to remove the empty space on both sides of a string
    Syntax: trim(variable name)
  5. strrev()
    It is used to reverse the string
    Syntax: strrev(variable name)
  6. strstr()
    it is used to search the first occurrence of string inside another string.
    Syntax: strstr(search, replace)
  7. stristr()
    same as strstr() but it was case sensitive.
    Syntax: strstr(search, replace)
  8. nl2br()
    Used to add line breaks (<br>) inside a string in place of  \n or \r
  9. str_split()
    used to split the string into an array
    Syntax: str-split(string [,length])
    lenghth is an optional, each character is stored as an array element default is 1.
  10. substr()
    used to collecting a part of String.
    syntax: substr(string, start index,[length])
  11. substr_count()
    for count occurrence of substring in main string
    syntax: str_count(string,substring,[start position],[length])
  12. str_repeat()
  13. used to repeating a string number of times.
    syntax: str-repeat(string, repeat count)
  14. strtolower()
    convert the string into lower case of alphabet.
    syntax: strtolower(string)
  15. strtoupper()
    convert the string into upper case of alphabet.
    syntax: strtoupper(string)
  16. strcasecmp()
    it was used to case insensitive comparison. this function returns “0” if both strings are equal otherwise it will return the numerical difference between first non matching characters.
    The value wil be +ve or -ve
    syntax:  strcasecmp(string1,string2)
  17. strcmp
    It was same as strcasecmp but it was case sensitive.
    it will return 0 if both are equal
    it will return 1 if string1>string2
    it will return -1 if string1<string2
  18. str_pad()
    it was used to add extra padding with specified length to the string.
    Syntax: str_pad(string,length,[pad string][pad_type])
    where PAD_TYPE is a constant of php it will be either of the php
    STR_PAD_LEFT(0)
    STR_PAD_RIGHT(1)
    STR_PAD_BOTH(2)
Example :

<?php
$x="psd2web ";

echo $x."<br>";
echo strlen($x)."<br>";
//echo str_replace(k,kk,$x)."<br>";
$str=" sunil \n kumar";
echo trim($str);
echo strlen($str)."<br>";
echo strrev($str)."<br>";
echo $z=$x.$str."<br>";
echo strstr($x,"i")."<br>";
echo nl2br($str)."<br>";
$k=str_split($x);
echo print_r($k)."<br>";
echo substr($x,3)."<br>";
echo substr_count($x,"u")."<br>";
echo str_repeat($x, 10)."<br>";
echo strtoupper($x)."<br>";
echo strtolower($x)."<br>";
echo strcmp("Sunil", "sunil")."<br>";
echo strcasecmp("Aunil", "Cunil")."<br>";
$new= str_pad($x,5);
echo $new.$x;

?>

more string functions…

You Might Also Like