PHP Functions

PHP chunk_split() Function Example

PHP Script:

<?php
echo chunk_split(“PHPTUTORS”,1,”_”);
echo “<br/ >”;
echo chunk_split(“Learn PHP on PHPTUTORS”,12,”…”);
?>

Output:

P_H_P_T_U_T_O_R_S_
Learn PHP on… PHPTUTORS…

Description:

chunk_split() function splits a string into a series of smaller chunks and append a specified string after each chunk as shown in above script example.

chunk_split function parameters:

1- String: String which is going to be spit into chunks
2- Chunk Length: Length of chunk [default is 76]
3- String: String which is appended at the end of each chunk

PHP parse_str() Function Example

PHP Script:

<?php
parse_str(“post=PHP%20parse%20str&blog=PHPTUTORS”);
echo $post;
echo “<br / >”;
echo $blog;
echo “<br / >”;
parse_str(“post=PHP%20parse%20str%20function&blog=PHPTUTORS”,$data_array);
print_r($data_array);
?>

Output:

PHP parse str
PHPTUTORS
Array ( [post] => PHP parse str function [blog] => PHPTUTORS )

Description:

parse_str() function parses a query string into variables.

parse_str function parameters:

1- String: Query string which is going to be parse into variables as shown in above script example.
2- Array: This parameter tells that query string data will be stored in an associative array as shown in above script example.

PHP strstr() Function Example

PHP Script:

<?php
echo strstr(“PHP str str function example on PHPTUTORS”,”PHPTUTORS”);
?>

Output:

PHPTUTORS

Description:

strstr() function searches for the first occurrence of a string inside another string.
This function returns the string from the matching point like PHPTUTORS in above example script or if the string is not found false will be return.
This function is case-sensitive.

strstr function parameters:

1- String: string to search.
2- Search string: Specify the string to search for. We can also provide a number so, characters ASCII value matching is performed with this number.

PHP str_repeat() Function Example

PHP Script:

<?php
echo str_repeat(“:.:”,10);
?>

Output:

:.::.::.::.::.::.::.::.::.::.:

Description:

str_repeat() function repeats the string to a specified number of times.

str_repeat function parameters:

1- String: string to repeat.
2- Repeat: Specify how many times the above string to repeat [0 or more].

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_shuffle() Function Example

PHP Script:

<?php
echo str_shuffle(“PHPTUTORS”);
?>

Output:

TSTPPOHRU

Description:

str_shuffle() function randomly shuffles all the characters of a string.

str_shuffle function parameters:

1- String: string to shuffle

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]




No comments:

Post a Comment