Category: Beginners

PHP helper function to replace placeholders in string

Below is the PHP helper function which can be use to replace placeholders in a specified string with their actual values:

function replace_placeholders($text, $placeholders = []) {

$placeholderPrefix = ':';

foreach ($placeholders as $key => $value) {

$text = str_replace($placeholderPrefix.$key, $value, $text);

}

return $text;

}

Usage:

$text = 'Learn beginners, intermediate and advance level :LANGUAGE and associated technologies by :NAME.';
$placeholders = ['NAME' => 'PHP Tutors', 'LANGUAGE' => 'PHP'];
echo replace_placeholders($text, $placeholders);
//Output:
Learn beginners, intermediate and advance level PHP and associated technologies by PHP Tutors.

Description:

  • $text is the parameter containing string in which we have placeholders :LANGUAGE and :NAME in the above example.
  • $placeholders is the parameter containing placeholders as an array key and actual values as an array values.
  • Using str_replace() PHP’s function we replace placeholders with their actual values like :LANGUAGE with PHP and :NAME with PHP Tutors as shown in above example.

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.