Sunday, January 21, 2018

PHP String Function for BCA,B.Sc(IT), MCA, M.Sc(IT) and All IT Students


--------------------------------------------------------------------------------------------------------------------------
Prepared By : Uday Shah (HOD-IT)
Contact No   : 7600044051
E-Mail          : rupareleducation@gmail.com



PHP String Function

No
Name
Description
1
chr()
This function returns a one character string containing character specified by ascii.
syntax: string chr(ASCII)
Ex: echo chr(65);
2
ord()
This function returns the ascii value of the characters of string.
syntax: int ord(string)
Ex: echo ord(“A”);
3
strtolower()
It returns string with all alphabetic characters converted into lower case.
syntax: string strtolower(string)
Ex: echo strtolower(“WELCOME”);
4
strtoupper()
It returns string with all alphabetic characters converted to uppercase.
syntax: string strtoupper(string)
Ex: echo strtoupper(“welcome”);
5
Strlen()
It returns the length of the given string.
syntax: int strlen(string)
Ex:echo strlen(“welcome”);
6
Ltrim()
It squeeze the white space from the beginning of the string.
              syntax: string ltrim(string)
                   ex: ltrim(“            welcome”);
7
Rtrim()
It squeeze the white space from the ending of a string.
syntax: string rtrim(string)
Ex: echo rtrim(“welcome           “);
8
trim()
Trim is same as ltrim and rtrim function but it squeeze space from both left side and right side.
syntax: string trim(string)
Ex:echo trim(“          welcome           “);
9
substr()
It returns the part of a string.
Syntax: substr(string, int start[,int length])
Substr returns the part of a string specified by the start & length parameters.
Ex: echo substr(“abcde”,0,2);
For instance in the string “abcde” the character or position 0(zero) is “a” character at the position 2 is “c” character and so on.
10
strcmp()
It compares two string if it returns <0 if str 1 is less than str 2; >0 if str 1 is greater than str2, and 0 if they are equal.
It is Binary safe string comparison.
This comparison is case sensitive.
Syntax: int strcmp(string1,string2)
Ex: echo strcmp(“Hello”,”hello”);
11
strcasecmp()
It is case insensitive string comparison.
Syntax:
Int strcasecmp(string1,string2)
Ex: $var1=”Hello”;
$var2=”hello”;
echo strcasecmp($var1,$var2);
12
strpos()
It is used to find position of first occurrence of a string.
It returns the numeric position of the first occurrence  in the string. If string not found it will return false.
Syntax: int strpos(string1, position)
Ex: <?php
          $newstring=”abcdef abcdef”;
          $pos=strpos($newstring,’a’);
          echo $pos;
          ?>
13
strrpos()
It is used to find position of last occurrence of a character in a string.
It returns the numeric position of the last occurrence in the string. If not found , it returns false.
Syntax: int strrpos(string1, chr);
Ex: <?php
          $newstring=”abcdef abcdef”;
          $pos=strrpos($newstring,’a’);
          echo $pos;
          ?>
14
Strstr()
It returns a part of a string from the first occurrence to the end of string.
This function is case sensitive.
Syntax: strstr(string,parameter)
Ex: <?php            $email=User@example.com;
          $domain=strstr($email,’@’);            echo $domain;
          ?>
15
stristr()
It returns all of the string from the first occurrence to the end.
It is case insensitive, means here you can use capital letters or small letters but the answer will be same.
Syntax: stristr(string,parameter);

Ex: <?php                      $email=User@example.com;
          $domain=stristr($email,’E’);
          echo $domain;
          ?>
16
str_replace()
It is used to replace all occurrence of the search string with the replacement string.
This function returns a string or an array with all occurrence of search in subject replaced with the given value.
In this function every parameter can be an array.
Syntax:str_replace(search string,replace string,count)

<?php
$vowels=array(“a”,”e”,”i”,”o”,”u”,”A”,”E”,”I”,”O”,”U”);
$x=str_replace($vowels,”*”,”Hello World of PHP”);
echo $x;
?>
17
strrev()
It is used to reverse a string.
Syntax: string strrev(string);
Ex:strrev(“Welcome”);
18
echo()
It is used to print the out put for one or more string.
Echo is not actually a function, so you are not require to use parameters with it.
Syntax:echo (string argu1);
Ex: echo “Hello world”;
19
print()
It is used to get output of a string. Print is not actually a real function.
So you are not require to use parenthesis with it.
It is also used to print a statement in multiple lines.
Syntax: print(string arg);
Ex: print “this spans
          Multiple lines The new
          Lines will be output as well.”;
20
Explode()
The explode() function breaks a string into an array.
Note: The "separator" parameter cannot be an empty string.
Syntax
explode(separator,string,limit)

<?php
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>
21
implode()
The implode() function returns a string from the elements of an array.
Note: The implode() function accept its parameters in either order. However, for consistency with explode(), you should use the documented order of arguments.

Syntax

implode(separator,array)

Example

<?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>
22
join()
The join() function returns a string from the elements of an array.
The join() function is an alias of the implode() function.

Syntax

join(separator, array)
Example
<?php
$arr=array('Hello','World!','Beautiful','Day!');
echo join(" ",$arr);
?>
23
md5()
The md5() function calculates the MD5 hash of a string.
The md5() function uses the Data Security, Inc. MD5 Message-Digest Algorithm.

Syntax

md5(string,raw)

 

Example

<?php
$str = "Hello";
echo md5($str);
?>
24
Str_split ()
The str_split () function splits a string into an array.

Syntax

str_split(string,length)

Example

<?php
print_r(str_split("Hello"));
?>
25
Str_shuffle ()
The str_shuffle() function randomly shuffles all the characters of a string.

Syntax

str_shuffle(string)

<?php

          $str=’abcdef’;
          $shuffled=str_shuffle($str);
          Echo $shuffled;
?>
26
Strcspn ()
The strcspn() function returns the number of characters (including white spaces) found in a string before any part of the specified characters are found.

Syntax

strcspn(string,char,start,length)

Example

Print the number of characters found in "Hello world!" before the character "w":
<?php
echo strcspn("Hello world!","w");
?>
27
strpbrk()
The strpbrk() function searches a string for any of the specified characters.
Note: This function is case-sensitive.
This function returns the rest of the string from where it found the first occurrence of a specified character, otherwise it returns FALSE.

Syntax

strpbrk(string,charlist)

Example

<?php
echo strpbrk("Hello world!","oe");
?>
28
substr_compare()
The substr_compare () function compares two strings from a specified start position.

Syntax

substr_compare (string1, string2, startpos, length,case)
Compare two strings, when start position in string1 for the comparison is 6th:
<?php
echo substr_compare("Hello world", "world",6);
?>
29
substr_count ()
The substr_count() function counts the number of times a substring occurs in a string.
Note: The substring is case-sensitive.

Syntax

substr_count(string,substring,start,length)
Count the number of times "world" occurs in the string:
<?php
echo substr_count("Hello world. The world is nice","world");
?>
30
ucfirst()
The ucfirst() function converts the first character of a string to uppercase.
Related functions:
  • lcfirst() - converts the first character of a string to lowercase
  • ucwords() - converts the first character of each word in a string to uppercase
  • strtoupper() - converts a string to uppercase
  • strtolower() - converts a string to lowercase

Syntax

ucfirst(string)

Example

Convert the first character of "hello" to uppercase:
<?php
echo ucfirst("hello world!");
?>
31
ucwords()
Convert the first character of each word to uppercase:
Syntax: ucwords(string)
<?php
echo ucwords("hello world");
?>


Please share and comment it... Thank You.

:: Best Of Luck :: 

https://myshopprime.com/uday.shah3/shop