Wednesday, 7 August 2013

20 Handy PHP String Functions

PHP is the widely used open source language especially for developing web applications. Strings in PHP play very important role. In this tutorial, You will find about 20 handy and most used PHP functions.

1. addcslashes()

This string function returns a string also with backslashes before characters and characters are listed as parameter.
Syntax:
1
<?php addcslashes($str, characters); ?>
Example:
1
2
3
4
<?php
$mystr = "How are you.";
echo addcslashes($mystr, 'r');
?>
Output:
1
How a\re you.

2. convert_uudecode()

This string function decodes a string that is uuencoded (in simple words it decodes a string which is encrypted by some security methods).
Syntax:
1
2
3
<?php
convert_uudecode($encrypted_string);
?>
Example:
1
2
3
4
5
<?php
$encrypted_string = "+2&]W($%R92!9;W4` `";

echo convert_uudecode($encrypted_string);
?>
Output:
1
How Are You

3. convert_uuencode()

This string function encodes a string.
Syntax:
1
2
3
<?php
convert_uuencode($encrypted_string);
?>
Example:
1
2
3
4
5
<?php
$encrypted_string = "How Are You";

echo convert_uuencode($encrypted_string);
?>
Output:
1
+2&]W($%R92!9;W4` `

4. count_chars()

This string function returns how many times a character (ASCII) appears in a string depending on the modes we specified.
By default, mode is 0 that returns an array with Key (ASCII Value) and Value (Number of Occurrences.
Mode 1 returns an array with same functionality as Mode 2, but it only lists appearance of characters which are greater than zero.
Mode 2 returns occurrence which are equal to zero.
Mode 3 returns string with difference characters used.
Mode 4 generates string of all unused characters.
Syntax:
1
count_chars($str, mode);
Example:
1
2
3
4
5
6
7
8
9
10
11
<?php
$mystr = "Hello Dear, How are you";

print_r(count_chars($mystr, 0));
print_r(count_chars($mystr, 1));
print_r(count_chars($mystr, 2));

echo count_chars($mystr, 3);
echo count_chars($mystr, 4);

?>

5. echo()

This function helps to output more than one strings. Parameters in this function are optional.
Syntax:
1
echo(string)

6. explode()

explode() explodes or break a string into an array. We can easily understand the functionality of this function by the name “explode”. explode() has three parameters, first 2 are required and third is optional.
Syntax:
1
explode(string_separator, $str, limit);
Example:
1
2
3
4
5
6
7
<?php

$mystr = "Hello Dear, How are you";

print_r(explode(" ", $mystr));

?>
Output:
1
Array ( [0] => Hello [1] => Dear, [2] => How [3] => are [4] => you )

7. implode()

implode() is a reverse of explode(), means it joins an array into a sring. It has two parameters, first one is optional and second one is required.
Syntax:
1
imlode(string_separator, array);
Example:
1
2
3
4
5
6
7
<?php

$arr = array("Hello", "Dear", "How", "Are", "You");

echo implode(" ", $arr);

?>
Output:
1
Hello Dear How Are You

8. md5()

md5() function calculates Message-Digest Algorithm (md5) hash of a string. It has two parameters, first one is required and second one is optional which was included in PHP 5.0.
Syntax:
1
md5($str, raw);
Example:
1
2
3
4
5
6
7
<?php

$mystr = "How Are You";

echo md5($mystr);

?>
Output:
1
9e227bb366c119c7f27a7115f0136f42

9. str_replace()

If you want to replace some specified characters with other characters in PHP, str_replace() is the best choice. It has many rules like:
  1. If we want to search a string in an array, it returns as an array.
  2. Find and Replace is performed with each array element as we want to search a string in an array.
  3. If we are finding an array and also replacing with it with an array, an empty string will be used as replace.
  4. In case, replace is a string and find is an array, then it uses replace sting as find value.
Syntax:
1
str_replace(find, replace, string, count);
Example:
1
2
3
<?php
echo str_replace("How", "Dear", "How Di");
?>
Output:
1
How Dear

10. str_split()

str_split() works like explode() in some manner, it is used for splitting a string into an array.
Syntax:
1
str_split($mystring, length);
Example:
1
2
3
<?php
print_r(str_split("What"));
?>
Output:
1
2
3
4
5
6
7
Array
(
[0] => W
[1] => h
[2] => a
[3] => T
)

11. str_word_count()

This function is used for counting the words in a string.
Syntax:
1
str_word_count(string, return, char);
Example:
1
2
3
<?php
echo str_word_count("What are you doing");
?>
Output:
1
4

12. strcmp()

This case-sensitive string function is used for comparing two strings. It returns “0″ if both strings are equal, “<0″ if first string is less than second string and “>0″ if first string is greater than second string.
Syntax:
1
strcmp(first string, second string);
Example:
1
2
3
<?php
echo strcmp("What Are You Doing", "What I Should Do");
?>
Output:
1
-1

13. strlen()

strlen() calculates the length of string based on extra spaces and characters used in a string.
Syntax:
1
strlen($str);
Example:
1
2
3
<?php
echo strlen("Oh, No!");
?>
Output:
1
7

14. strrpos()

strrpos() is used to find the position of the last occurrence string inside another string.
Syntax:
1
strrpos(strong, find, start)
Example:
1
2
3
4
5
6
7
8
<?php
echo strrpos("Where are you going?", "go");
?>
[code]

<strong>Output:</strong>

[code type="php"]14

15. strstr()

strstr() finds the first occurrence of a string in a string, if found it returns at the matching point else it returns 0.
Syntax:
1
strstr();
Example:
1
2
3
<?php
echo strstr('Hi What', 'Hi');
?>
Output:
1
Hi What

16. strtolower()

This function converts a string to lowercase (It converts only letters from A-Z or a-z).
Syntax:
1
strtolower($str);
Example:
1
2
3
<?php
echo strtolower("Now WhaT are");
?>
Output:
1
now what are

17. strtoupper()

This function converts a string to uppercase letters.
Syntax:
1
strtoupper($str)
Example:
1
2
3
<?php
echo strtoupper("Yes");
?>
Output:
1
YES

18. substr()

This function split up a part from a string according to given criteria specified in the parameters.
Syntax:
1
substr($str, start, length)
Example:
1
2
3
<?php
echo substr("Where I Can Find You", 7);
?>
Output:
1
Can Find You

19. trim()

This function erases predefined characters and whitespaces from every side of a string. Predefined characters are like: \0, \t, \n, \xob, \r etc.
Syntax:
1
trim($str, chars)
Example:
1
2
3
4
<?php
$mystr = "Hey dude, I'm going with you";
echo trim($mystr);
?>

20. wordwrap()

If you want to cut a string after it reaches to a specific length, wordwrap() is the best choice for achieving this.
Syntax:
1
wordwrap($str, width, break, cut)
Example:
1
2
3
4
5
6
<?php

$str = "Check the example: Supercalifragulistic";

echo wordwrap($str,9);
?>
Output:
Browser will display out like this
1
Check the example: Supercalifragulistic
But after checking the page source, output will be something like below:
1
2
3
Check the
example:
Supercalifragulistic

No comments:

Post a Comment