1. addslashes

* Returns a string with backslashes before characters such as single quote ('), double quote ("), backslash (\) and NULL (the NULL byte).
* This is mainly used for escaping database queries.
* This function complements stripslashes().

Syntax string addslashes(string str)
Example:
  	
<?php
$str = "What's your name?";
echo addslashes($str);
$str = "Is your name O'reilly??";
echo "
".addslashes($str); ?>
Output :
What\'s your name?
Is your name O\'reilly?

2. stripslashes

Returns a string with backslashes stripped off.(\' becomes ' and so on.) Double backslashes (\\) are made into a single backslash (\).)

Syntax int string stripslashes(string str)
Example:
  	
<?php
$str = "What\'s your name?";
echo stripslashes($str);
$str = "Is your name O\'reilly?";
echo "
".stripslashes($str); ?>
Output :
What's your name?
Is your name O'reilly?

3. Chr

* Returns a one-character string containing the character specified by ascii.
* This function complements ord().

Syntax string chr(int ascii)
Example:
  	
<?php
echo chr(65);
echo "
".chr(98); ?>
Output :
A b

4. ord

Returns ascii value of first character of string

Syntax int ord(string string)
Example:
  	
<?php
echo ord("Preeti");
echo ord("b");
?>
	
Output :
84 (Note: 84 is the ASCII value of 'T',which is the first character of input string 'Tapas')
98

5. trim

* This function returns a string with stripped characters specified using the charlist parameter from the beginning and end of string.
* Without the second parameter, trim() will strip these characters: ' ' (ASCII 32 (0x20)), an ordinary space. '\t' (ASCII 9 (0x09)), a tab. '\n' (ASCII 10 (0x0A)), a new line (line feed).

Syntax string trim(string str[,string charlist])
Example:
  	
<?php
$str = "\t\tThese are a few words :) ... ";
echo trim($str);
$str = trim($str);
echo trim($str,'.');
?>
	
Output :
These are a few words :) ...
These are a few words :)

6. str_pad

* This functions returns the input string padded on the left, the right, or both sides to the specified padding length and padded with characters from pad_string(if not supplied then spaces) up to the limit.
* Parameters:
input - The input string.

pad_length:
* If the value of pad_length is negative or less than the length of the input string, no padding takes place.
pad_string:
* The pad_string may be truncated if the required number of padding characters can't be evenly divided by the pad_string's length.
pad_type:
* Optional argument pad_type can be STR_PAD_RIGHT, STR_PAD_LEFT, STR_PAD_BOTH. If pad_type is not specified it is assumed to be STR_PAD_RIGHT.
Example:
  	
<?php
$input = "Afixi";
echo str_pad($input, 10);                    
echo str_pad($input, 10, "-=", STR_PAD_LEFT);
echo str_pad($input, 10, ".", STR_PAD_BOTH);
echo str_pad($input, 6 , "___");
?>
	
Output :
Afixi
-=-=-Afixi
..Afixi...
Afixi_

7. str_replace

* This function returns a string or an array with all occurrences of search in subject replaced with the given replace value.
* If search or replace are arrays, their elements are processed first to last.
* If subject is an array, then the search and replace is performed with every entry of subject, and the return value is an array as well.
* Count if passed, this will hold the number of matched and replaced needles.

Syntax mixed str_replace(mixed search,mixed replace, mixed subject[,int &count] )
Example:
  	
<?php
$search='x'; 
$replaceWith='php'; 
$str='x-myadmin'; 
$result=str_replace($search, $replaceWith, $str, $count);
print $result;
print $count;
?>
	
Output :
php-myadmin
1

8. strstr

* Returns part of haystack string from the first occurrence of needle to the end of haystack after a case-sensitive search.
* If needle not found returns false.
* Parameters :
     -> haystack : (Required)The input string.
     -> needle : (Required)If not a string, it is converted to an integer and applied as the ordinal value of a character.
     -> before_needle :(Optional) If TRUE, strstr() returns the part of the haystack before the first occurrence of the needle (excluding the needle). By default it is FALSE.
* For case-insensitive searches, use stristr().

Syntax string strstr(string haystack,string needle,bool before_needle)
Example:
  	
<?php
$email = 'name@example.com';
$domain = strstr($email, '@');
echo $domain;

echo "
".strstr("Hello world!",111); //In this example we will search a string for the ASCII value of "o": ?>
Output :
@example.com
o world!

9. strip_tags

* This function returns a string with all HTML and PHP tags stripped from a given strings.

Syntax string strip_tags(string,allow)
* string - (Required) Specifies the string to check.
* allow - (Optional) Specifies allowable tags. These tags will not be removed
Example:
  	
<?php
echo strip_tags("Hello world!");
echo "
"; echo strip_tags("Hello world!","<b>"); ?>
Output :

Hello world!
Hello world!

10. strlen

Returns the length of the given string.

Syntax int strlen(string string)
Example:
  	
<?php
$str = 'abcdef';
echo strlen($str);
$str = 'abc def';
echo "
".strlen($str); ?>
Output :
6
7

11. strtolower

* Returns string with all alphabetic characters converted to lowercase.
* This function complements strtoupper().

Syntax string strtolower(string string)

12. strtoupper

Returns string with all alphabetic characters converted to uppercase.

Syntax string strtoupper(string string)
Example:
  	
<?php
$str = "Mary Had A Little Lamb and She LOVED It So";
$str = strtolower($str);
echo $str;
echo "
".strtoupper($str); ?>
Output :
mary had a little lamb and she loved it so
MARY HAD A LITTLE LAMB AND SHE LOVED IT SO

13. substr

Returns the portion of string specified by the start and length parameters.
If start is non-negative, the returned string will start at the start'th position in string, counting from zero.
If start is negative, the returned string will start at the start'th character from the end of string.

Syntax string substr(string string,int start[,int length])
Example:
  	
<?php
$rest = substr("abcdef", 1);echo $rest;
$rest = substr("abcdef", 1, 3);echo $rest;
$rest = substr("abcdef", 0, 4);echo $rest;
$rest = substr("abcdef", 0, 8);echo $rest;
//Using Negative Start:
$rest = substr("abcdef", -1);echo $rest;
$rest = substr("abcdef", -2);echo $rest;
$rest = substr("abcdef", -3, 1);echo $rest;
?>
	
Output :
bcdef
bcd
abcd
abcdef
Using Negative Start:
f
ef
d

14. ucfirst

Returns a string with the first character of string capitalized, if that character is alphabetic.

Syntax string ucfirst(string str)
Example:
  	
<?php
$foo = 'hello world!';
echo $foo;
echo ucfirst($foo);
$bar = 'HELLO WORLD!';
echo $bar;
$bar = ucfirst($bar);
$bar = ucfirst(strtolower($bar));
echo $bar;
?>
	
Output :
hello world!
Hello world!
Hello world!

15. ucwords

Returns a string with the first character of each word in string capitalized, if that character is alphabetic.

Syntax string ucwords(string str)
Example:
  	
<?php
$foo = 'hello world!';
echo $foo;
$foo = ucwords($foo);
echo $foo;
$bar = 'HELLO WORLD!';
$bar = ucwords($bar);
$bar = ucwords(strtolower($bar));
echo $bar;
?>
	
Output :
hello world!
Hello World!
Hello World!

16. strcmp

Binary safe string comparison

Syntax int strcmp(string str1,string str2)

Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.Comparison is case sensitive.
For case-insensitive comparison use strcasecmp()(Syntax:int strcasecmp(string str1,string str2))

Example:
  	
<?php
$var1 = "Hello";
$var2 = "hello";
if (strcmp($var1, $var2) == 0) {
    echo '$var1 is equal to $var2 in a
        case-sensitive string comparison';
} else {
    echo '$var1 is not equal to $var2 in a
        case-sensitive string comparison';
}
?>
	
Output :
$var1 is not equal to $var2 in a case-sensitive
string cpmparision

17. strrev

Returns the reversed string.

Syntax strrev(string str)
Example:
  	
<?php
$str = "Hello world!";
echo strrev($str);
?>
	
Output :
!dlrow olleH

18. explode

Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter.

Syntax explode(string delimiter, $str);
Example:
  	
<?php
$num_str ="one,two,three";
$num = explode(",", $num_str);
echo "<pre>";
print_r($num);
$num_str ="This is cow";
$num = explode(" ", $num_str);
print_r($num);
?>
	
Output :
Array
(
    [0] => one
    [1] => two
    [2] => three
)     
Array
(
    [0] => This
    [1] => is
    [2] => cow
)     

19. implode

Returns a string containing a string representation of all the array elements in the same order, with the glue string between each element.

Syntax implode(string glue,$arr)
Example:
  	
<?php
$detail=Array("Afixi","Technologies",9853247533);
$detail_string=implode(",",$detail);
print $detail_string; 
echo "
"; $detail=Array("Afixi","Technologies","Pvt.","Ltd."); $detail_string=implode(" ",$detail); print $detail_string; ?>
Output :
Afixi,Technologies,9853247533
Afixi Technologies Pvt. Ltd.

20. strpos

Find position of first occurrence of a string

Syntax int strpos(string,find,start)
Example:
  	
<?php
$mystring = 'abc';
$findme   = 'b';
$pos = strpos($mystring, $findme);
if ($pos === false) {
   echo "The string '$findme' was not found in the string '$mystring'";
} else {
   echo "The string '$findme' was found in the string '$mystring'";
   echo " and exists at position $pos";
}
?>
	
Output :
The string 'b' was found in the string 'abc' and exists at position 1

21. wordwrap

Wraps a string to a given number of characters

Syntax wordwrap(string,width,break,cut)
* string - Required. Specifies the string to break up into lines
* width - Optional. Specifies the maximum line width. Default is 75
* break - Optional. Specifies the characters to use as break. Default is "\n"
* cut - Optional. Specifies whether words longer than the specified width should be wrapped. Default is FALSE (no-wrap)
Example:
  	
<?php
$str = "An example on a long word is: Supercalifragulistic";
echo wordwrap($str,15,"
\n",TRUE); ?>
Output :
An example on a
long word is:
Supercalifragul
istic

Questions

  1. How to check whether a string contains HTML or not ? (ans : use condition if (strlen($myString) != strlen(strip_tags($myString)) )
  2. How to remove spaces from arround a string ?
  3. How to convert a string to array ?
  4. Find how many characters in string "Hello String"?
  5. Write the code for getting the ascii key of 'A' in "Afixi" and what will be the output?
  6. What is the difference between explode() and implode() ?
  7. How can i get the extension of an image file?
  8. What is the difference between strstr() and stristr()?
  9. How to get 'name' from the given string 'name@example.com' ?
After editing Click here:
Output:
Hello World