This tutorial is adapted from: http://myphpscriptz.com/php-scripts-tutorials/11-most-important-string-functions-php-a-programmer-should-know/
STEPS
1) substr()
This function returns the part of the string as an output.
Syntax :
1
|
substr(<string>,<start>,[<length>]);
|
Explanation :
String : It is mandatory parameter. The string from which the part is to be extracted is mentioned here.
Start : The start in the string from which the characters are to be extracted
- Positive number – Start at a specified position in the string
- Negative number – Start at a specified position from the end of the string
- 0 – Start at the first character in string
Length : It is an optional parameter. It specifies the length of the string which is to be extracted.
- Positive number – The length to be returned from the start parameter
- Negative number – The length to be returned from the end of the string
Example 1:
1
|
<?php echo substr("Hello world",6); ?> //Returns world
|
Example 2 :
1
|
<?php echo substr("Hello world",6,4); ?> // Returns worl
|
Example 3 :
1
|
<?php echo substr("Hello world", -1); ?> // Returns d
|
Example 4:
1
|
<?php echo substr("Hello world", -3, -1); ?> // Returns rl
|
2) strlen()
This function returns the length of the string
Syntax :
1
|
strlen(<string>);
|
Explanation:
String : It is mandatory field. The string whose length is to be found out is mentioned here.
Example 1:
1
|
<?php echo strlen("Hello world"); ?> // Returns 11
|
3) trim()
This function removes the whitespaces from both start and the end of the string.
Syntax :
1
|
trim(<string>);
|
Explanation :
String : It is mandatory field. The string of which the whitespaces are to be removed is passed as parameter.
Example 1:
1
|
<?php echo trim( " Hello World "); ?> // returns Hello World. If you go view source then you can see that there are no whitespaces.
|
4) ltrim()
This function removes the whitespaces from the left part of the string.
Syntax :
1
|
ltrim(<string>);
|
Explanation :
String : It is mandatory field. The string of which the whitespaces are to be removed from left side is passed as parameter.
Example 1:
1
|
<?php echo ltrim( " Hello World "); ?> // returns Hello World. If you go view source then you can see that there are no whitespaces on left side but there are spaces on right side.
|
5) rtrim()
This function removes the whitespaces from the right part of the string.
Syntax :
1
|
rtrim(<string>);
|
Explanation :
String : It is mandatory field. The string of which the whitespaces are to be removed from right side is passed as parameter.
Example 1:
1
|
<?php echo rtrim( " Hello World "); ?> // returns Hello World. If you go view source then you can see that there are no whitespaces on right side but there are spaces on left side
|
6) strtolower()
This function converts the string to lower case
Syntax :
1
|
strtolower(<string>);
|
Explanation :
String : It is mandatory field. The string which is to be converted to lower case is passed here.
Example 1:
1
|
<?php echo strtolower("HELLO WORLD"); ?> // Returns hello world
|
7) strtoupper()
This function converts the string to upper case
Syntax :
1
|
strtoupper(<string>);
|
Explanation :
String : It is mandatory field. The string which is to be converted to upper case is passed here.
Example 1:
1
|
<?php echo strtoupper("hello world"); ?> // Returns HELLO WORLD
|
8) str_replace()
The str_replace() function replaces some characters with some other characters in a string.
This function works by the following rules:
- If the string to be searched is an array, it returns an array
- If the string to be searched is an array, find and replace is performed with every array element
- If both find and replace are arrays, and replace has fewer elements than 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 :
1
|
str_replace(<search>,<replace>,<string/array>,[<count>]);
|
Explanation :
Search : It is mandatory . The string or value to be searched comes here.
Replace : It is mandatory. The string or value to be replaced comes here.
String/Array : It is mandatory. The string or array in which the value is to be found out comes here.
Count : It is optional. It counts the number of replacements to be done.
Example 1:
1
|
<?php echo str_replace("world","Peter","Hello world"); ?>// Returns Hello Peter
|
Example 2:
01
|
<?php
|
02
|
$arr = array("blue","red","green","yellow");
|
03
|
print_r(str_replace("red","pink",$arr,$i));
|
04
|
echo "Replacements: $i";
|
05
|
?>
|
06
|
07
|
/*
|
08
|
09
|
Output :
|
10
|
11
|
Array
|
12
|
(
|
13
|
[0] => blue
|
14
|
[1] => pink
|
15
|
[2] => green
|
16
|
[3] => yellow
|
17
|
)
|
18
|
Replacements: 1
|
19
|
20
|
*/
|
Example 3:
01
|
<?php
|
02
|
03
|
$phrase = "You should eat fruits, vegetables, and fiber every day.";
|
04
|
$healthy = array("fruits", "vegetables", "fiber");
|
05
|
$yummy = array("pizza", "beer", "ice cream");
|
06
|
$newphrase = str_replace($healthy, $yummy, $phrase);
|
07
|
08
|
?><strong> </strong>
|
09
|
10
|
/*
|
11
|
12
|
Output :
|
13
|
14
|
You should eat pizza, beer, and ice cream every day
|
15
|
16
|
*/
|
9) strcmp()
The strcmp() function compares two strings.
This function returns:
- 0 – if the two strings are equal
- <0 – if string1 is less than string2
- >0 – if string1 is greater than string2
Syntax :
1
|
strcmp(<string1>,<string2>);
|
Explanation :
String1 : It is mandatory. The first string comes here.
String 2 : It is mandatory. The Second string comes here.
Example 1:
1
|
<?php echo strcmp("Hello world!","Hello world!"); ?> //Returns 0
|
Note: The strcmp() function is binary safe and case-sensitive. For case insensitive comparison you can use strcasecmp(<string1>,<string2>); function. It is similar to strcmp() function.
10) explode()
This function breaks the string into array on the basis of delimiter passed.
Syntax:
1
|
explode(<delimeter>,<string>,[<limit>]);
|
Explanation:
Delimeter: It is mandatory field. It specifies where to break the string.
String: It is mandatory. It specifies the string to split.
Limit : It is optional. It specifies the maximum number of array elements to return.
Example 1:
01
|
<?php
|
02
|
$str = "Hello world. It's a beautiful day.";
|
03
|
print_r (explode(" ",$str));
|
04
|
?>
|
05
|
06
|
/* Output :
|
07
|
08
|
Array
|
09
|
(
|
10
|
[0] => Hello
|
11
|
[1] => world.
|
12
|
[2] => It's
|
13
|
[3] => a
|
14
|
[4] => beautiful
|
15
|
[5] => day.
|
16
|
)
|
17
|
18
|
*/
|
11) implode()
This function join array elements with a string on the basis of delimiter passed.
Syntax:
1
|
implode(<delim>,<array>);
|
Explanation:
Delimiter: It is mandatory field. It specifies what to put between the array elements. Default is “” (an empty string).
Array: It is mandatory field. It specifies the array to join to a string.
Example 1:
01
|
<?php
|
02
|
$arr = array('Hello','World!','Beautiful','Day!');
|
03
|
echo implode(" ",$arr);
|
04
|
?>
|
05
|
06
|
/*
|
07
|
08
|
Output:
|
09
|
10
|
Hello World! Beautiful Day!
|
11
|
12
|
*/
|
-----
No comments:
Post a Comment