Monday 28 November 2011

String Function In PHP


Ø       chr()

  • This function is used to convert the ascii code values into its character value.

Syntax

                        string chr(int ascii)

Example

<?php
          echo chr(65);
          ?>

Output  

                  A

Ø       ord()

  • Returns the ascii value of the first character of string.

Syntax

                        Int ord(string $string)

Example

<?php
          echo ord(“H”);
          ?>

Output  

                  104

Ø       strtolower()

  • Return string with all alphabetic character converted to lowercase.

Syntax

                        string strtolower(string $str)

Example

<?php
          $str="Hello World";
          echo strtolower($str);
          ?>

Output  

                  hello world

Ø       strtoupper()

  • Return string with all alphabetic character converted to uppercase.

Syntax

                        string strtoupper(string $str)

Example

<?php
          $str="Hello World";
          echo strtoupper($str);
          ?>

Output  

                  HELLO WORLD

Ø       strlen()

  • Return the length of the given string and 0 if the string is empty.

Syntax

                        int strlen(string $str)

Example

<?php
          $str="Hello World";
          echo strlen($str);
          ?>

Output  

                  11

Ø       ltrim()

  • Strip white space or other character from the beginning of the string.

Syntax

                        string ltrim(string $str[,string $charlist])

Parameter
Description
Str
The input string
Charlist
You can also specify the character you want to strip means of the character list parameter.


Example

<?php
          $str="\t Hello World";
          var_dump(ltrim($str));
          ?>

Output  

              string(11) "Hello World"

Ø       rtrim()

  • Strip white space from the end of a string.

Syntax

                        string rtrim(string $str[,string $charlist])

Example

<?php
          $str="\t Hello World ";
          var_dump(rtrim($str));
          ?>

Output  

              string(13) " Hello World"

Ø       trim()

  • The trim function removes a white space or other pre character striped from both side of a string.
Syntax

                        string trim(string $str[,string $charlist])

Example

<?php
          $str="\t Hello World  ";
          var_dump(trim($str));
          ?>

Output  

              string(11) "Hello World"

Ø       substr()

  • The substr() returns a part of a string.

Syntax

                        string substr(string string, int start[, int length])
         
Parameter
Description
String
Required. Specify the string to return a part of it.
Start
Required, specifies where to start in the string.
1. A positive number start at a specified position in the string.
2. negative number start at a specified position from the end of the string
3. Zero start at the first character in string.
Length
Optional, specifies the length of the return string default is the end of the string.

Example

<?php
          $str="Hello World";
          echo substr($str,5);
          ?>

Output  

              World

Ø       strcmp()

  • The strcmp() compare two string.

This function returns.

1.     zero : if the two string are equal.
2.     <0 : if string1 is less than string2.
3.     >0 : if string2 is greater than string2.

  • Note: The strcmp() Is case sensitive function.

Syntax

                        int strcmp(string string1, string string2)

Example

<?php
          $str1 ="Hello World";
          $str2 ="Hello World";
          echo strcmp($str1,$str2);
          ?>

Output  

              0

Ø       strcasecmp()

  • The strcasecmp() compare two string.

This function returns.

1. zero : if the two string are equal.
2. <0 : if string1 is less than string2.
3.     >0 : if string2 is greater than string2.

  • Note: The strcasecmp() Is case insensitive function.

Syntax

                        int strcasecmp(string string1, string string2)

Example

<?php
          $str1 ="Hello";
          $str2 ="hello";
          echo strcasecmp($str1,$str2);
          ?>

Output  

              0

Ø       strpos()

  • Find position of first occurrence of a string.

Syntax

                        int strpos(string, find, start)


Parameter
Description
String
Required, specify the string to search.
Find
Required, specify the string to find.
Start
Optional, specify where to begin the search.


  • Note: The strpos() Is case sensitive function.

Example

<?php
          $str ="Hello";
          echo strpos($str,"e");
          ?>

Output  

              1

Ø       strrpos()

  • Find the position of last occurrence of a string.

Syntax

                        int strrpos(string, find, start)

Parameter
Description
String
Required, specify the string to search.
Find
Required, specify the string to find.
Start
Optional, specify where to begin the search.


Example

<?php
          $str ="Hello";
          echo strrpos($str,"l");
          ?>

Output  

              3

Ø       strstr()

  • The strstr() search for the first occurrence of the string inside another string.
  • The strstr() is case sensitive.

Syntax

                        string strstr(string, search)

Parameter
Description
String
Required, specify the string to search.
Search
Required, specify the string to search.

Example

<?php
          $str1 ="HeLlo world";
          $str2 ="l";
          echo strstr($str1,$str2);
          ?>

Output  

              lo world

Ø       stristr()

  • The strstr() search for the first occurrence of the string inside another string.
  • The strstr() is case insensitive.

Syntax

                        string strstr(string, search)

Parameter
Description
String
Required, specify the string to search.
Search
Required, specify the string to search.

Example

<?php
          $str1 ="HeLlo world";
          $str2 ="l";
          echo strstr($str1,$str2);
          ?>

Output  

              Llo world

Ø       str_replace()

  • The str_replace() replace some character with some other character in a string.
             
This function works by the following rules.

  • If the string to be search is an array, it retunes an array.
  • If the string to be search is an array, find and replace is performed with every array element.
  • If both find and replace are array and replace has fewer element then find, an empty string will be used as replace.
  • If find is an array and replace is a string, the replace string will be used for every find value.

Syntax

                   mixed str_replace(find, replace, string, [count])

Parameter
Description
Find
Required, specify the value to find.
Replace
Required, specify the value to replace value in find.
String
Required, specify the string to be search.
Count
Optional, a variable that counts the no of replacement.

Example

<?php
          $arr =array("black","pink","white");
          $str= str_replace("white","blue",$arr,$i);
          print_r($str);
          echo "<br/> Replacement : $i";
          ?>

Output  

Array([0]=>black[1]=>pink[2]=>blue)
Replacement : 1

Ø       strrev()

  • The strrev() reverse a string.

Syntax

                   string strrev(string $string)

Example

<?php
          $arr =array("black","pink","white");
          $str= str_replace("white","blue",$arr,$i);
          print_r($str);
          echo "<br/> Replacement : $i";
          ?>

Output  

dlroW olleH

Ø       echo()

  • The echo output one or more string.

Syntax

                   void echo(string $string)

Example

<?php
          echo "Hello World";
          ?>

Output  

Hello World

  • Note : The  echo() is not actually a function so you are not required to use parentheses with it.

Ø       print()

  • The print() output one or more string.

Syntax

                   void print(string $string)

Example

<?php
          $str= "Hello World";
          print ($str);
?>

Output  

Hello World

  • Note : The  print() is not actually a function so you are not required to use parentheses with it.

Posted By : Hemangi Zala

No comments:

Post a Comment