Category: PHP String Functions

PHP substr_count() Function Example

PHP Script:

<?php
echo substr_count(“PHPTUTORS a platform for learning PHP”,”PHP”);
echo “<br >”;
echo substr_count(“PHPTUTORS a platform for learning PHP”,”PHP”,33,3);
?>

Output:

2
1

Description:

substr_count() function counts the number of times a substring occurs in a string.

substr_count function parameters:

1- String: String containing substring to search for.
2- Substring: Substring which is going to be search in above string.
3- Start: From where to start searching a substring.
4- Length: Start search from above specified start position and end search after this LENGTH completion.
For e.g. in the above example 33 is the starting position of search and 3 is the length of the search so with in this boundary substring search.

PHP strrev() Function Example

PHP Script:

<?php
echo strrev(“PHP TUTORS”);
?>

Output:

SROTUT PHP

Description:

strrev() function use to reverse a string.

strrev function parameters:

1- String: string to reverse

PHP strip_tags() Function Example

PHP Script:

<?php
echo strip_tags(“<b >PHP TUTORS </b >”);
echo “<br >”;
echo strip_tags(“<i > <b >PHP TUTORS </b > </i >”,”<b >”);
?>

Output:

PHP TUTORS
PHP TUTORS

Description:

strip_tags() function strips tags of HTML, XML or PHP from string.

strip_tags function parameters:

1- String: string from which tags are strip
2- Allowed tags: Specifies allowable tags. These tags will not be removed

PHP str_split() Function Example

PHP Script:

<?php
$str = “phptutors” ;
print_r(str_split($str));
echo “<br >”;
print_r(str_split($str,2));
echo “<br >”;
print_r(str_split($str,strlen($str)));
?>

Output:

Array ( [0] => p [1] => h [2] => p [3] => t [4] => u [5] => t [6] => o [7] => r [8] => s )
Array ( [0] => ph [1] => pt [2] => ut [3] => or [4] => s )
Array ( [0] => phptutors )

Description:

str_split() function splits a string into an array.
If the length < 1 then false will return.
If the length = specified string length then whole string will be returned as an array element.

str_split function parameters:

1- String: string to split
2- Length: length of each array element [default = 1]

PHP similar_text() Function Example

PHP Script:

<?php
echo similar_text(“WELCOME TO PHP TUTORS”,”PHP TUTORS”);
echo “<br >”;
similar_text(“WELCOME TO PHP TUTORS”,”PHP TUTORS”,$percent);
echo $percent;
?>

Output:

10
64.516129032258

Description:

similar_text() function returns the number of matching characters of two strings.
It can also calculate the similarity of the two strings in percentage.

similar_text function parameters:

1- String1: first string to be compared
2- String2: second string to be compared
3- Percent: a variable name for storing the similarity in percentage

PHP implode() Function Example

PHP Script:

<?php
$array_to_implode = array(‘PHP’, ‘TUTOTRS’, ‘IMPLODE’, ‘FUNCTION’, ‘EXAMPLE’);
$string = implode(” “,$array_to_implode);
echo $string;
?>

Output:

PHP TUTOTRS IMPLODE FUNCTION EXAMPLE

Description:

PHP implode() function returns a string from the elements of an array joined by specified separator.
[In the above example we use SPACE as a separator]

PHP implode function parameters:

1- Separator: What to put between the Array elements when forming string
2- Array: The Array from which string is formed