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;



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


  • $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 imagecreatefromjpeg() Premature end of JPEG file issue

You can check the solution below to resolve this issue related to PHP GD Library function imagecreatefromjpeg():


You can set default value of gd.jpeg_ignore_warning=1 in php.ini
You can set it like this ini_set('gd.jpeg_ignore_warning', true); in your PHP script before calling imagecreatefromjpeg()

After implementing any of the above, GD Library will ignore the error where it use to fail and imagecreatefromjpeg() will return an image resource identifier.

Note: In PHP 7.1.0 the default value of gd.jpeg_ignore_warning has been changed from 0 to 1.


How to run the MongoDB as a Windows Service

Making logs and data folders:

1) Open command prompt in administrative mode
2) md F:\mongodb\log
3) md F:\mongodb\data
4) md F:\mongodb\data\db

Creating config file and add logpath and dbpath in it:

1) echo logpath=F:\mongodb\log\mongo.log > F:\mongodb\mongod.cfg
2) dbpath=F:\mongodb\data\db [add this to the next line in mongod.cfg]

Installing the MongoDB service using the above config file [mongod.cfg]:

C:\>F:\mongodb\bin\mongod.exe –config F:\mongodb\mongod.cfg –install
all output going to: F:\mongodb\log\mongo.log
log file [F:\mongodb\log\mongo.log] exists; copied to temporary file [F:\mongodb\log\mongo.log.2013-11-16T18-26-32]

Run the MongoDB service:

C:\>net start MongoDB

The Mongo DB service was started successfully.

Stop the MongoDB service:

C:\>net stop MongoDB

Removing the MongoDB service:

C:\>F:\mongodb\bin\mongod.exe –remove

PHP str_replace() Function Example

PHP Script:


//Converting relative urls into absolute urls | PHP Tutors

$base_url = '';
$anchors[0] = '<a href="tutorscommunity/">PHP Tutors Facebook Page Link </a>';
$anchors[1] = '<a href="groups/ptutors/">PHP Tutors Facebook Group Link </a>';

foreach($anchors as $val)
if(strpos($val,$base_url) === false)
echo str_replace('href="','href="'.$base_url,$val)."<br>";
echo $val."<br>";


PHP Tutors Facebook Page Link
PHP Tutors Facebook Group Link


str_replace() function replaces the specified string with other specified string with in a string.

str_replace() function parameters:

1- Find_string: Specify the string which is going to be replace
2- Replace_string: Specify the string which replace the above find_string in searched_string
3- Searched_String: Specify the string which is going to be searched

PHP shuffle() Function Example

PHP Script:

$sample_array = array("a" => "Samsung", "b" => "Iphone", "c" => "HTC", "d" => "Nokia", "e" => "Huawei");



Array ( [0] => Huawei [1] => Samsung [2] => HTC [3] => Iphone [4] => Nokia )


PHP shuffle() function is used to randomly reorder the elements in an array. And the existing keys of an array will be removed and new keys are assigned for the elements in an array.
This function returns TRUE on success, or FALSE on failure.

PHP shuffle() function parameters:

1- Array (Required)

PHP extract() Function Example

PHP Script:

$a = 'Carrot';
$sample_array = array("a" => "Apple","b" => "Banana", "c" => "Grapes");
// Extraction Rule = EXTR_OVERWRITE [Default]
echo 'EXTR_OVERWRITE: $a = '.$a.' $b = '.$b.' $c = '.$c;
echo "< br/ >";

$a = 'Carrot';
// Extraction Rule = EXTR_SKIP
echo 'EXTR_SKIP: $a = '.$a.' $b = '.$b.' $c = '.$c;
echo "< br/ >";

$a = 'Carrot';
// Extraction Rule = EXTR_PREFIX_SAME
echo 'EXTR_PREFIX_SAME: $a = '.$a.' $b = '.$b.' $c = '.$c.' $copy1_a = '.$copy1_a;
echo "< br/ >";

$a = 'Carrot';
// Extraction Rule = EXTR_PREFIX_ALL
echo 'EXTR_PREFIX_ALL: $copy2_a = '.$copy2_a.' $copy2_b = '.$copy2_b.' $copy2_c = '.$copy2_c;
echo "< br/ >";

$a = 'Carrot';
$sample_array2 = array("1" => "Apple","b" => "Banana", "c" => "Grapes");
// Extraction Rule = EXTR_PREFIX_INVALID
echo 'EXTR_PREFIX_INVALID: $copy3_1 = '.$copy3_1.' $b = '.$b.' $c = '.$c;
echo "< br/ >";

$a = 'Carrot';
$sample_array3 = array("a" => "Cherry","b" => "Banana", "c" => "Grapes");
// Extraction Rule = EXTR_IF_EXISTS
echo 'EXTR_IF_EXISTS: $a = '.$a.' $b = '.$b.' $c = '.$c;
echo "< br/ >";

$a = 'Carrot';
// Extraction Rule = EXTR_PREFIX_IF_EXISTS
echo 'EXTR_PREFIX_IF_EXISTS: $copy4_a = '.$copy4_a.' $b = '.$b.' $c = '.$c;
echo "< br/ >";

$a = 'Carrot';
// Extraction Rule = EXTR_REFS
echo 'EXTR_REFS: $a = '.$a.' $b = '.$b.' $c = '.$c;


EXTR_OVERWRITE: $a = Apple $b = Banana $c = Grapes
EXTR_SKIP: $a = Carrot $b = Banana $c = Grapes
EXTR_PREFIX_SAME: $a = Carrot $b = Banana $c = Grapes $copy1_a = Apple
EXTR_PREFIX_ALL: $copy2_a = Apple $copy2_b = Banana $copy2_c = Grapes
EXTR_PREFIX_INVALID: $copy3_1 = Apple $b = Banana $c = Grapes
EXTR_IF_EXISTS: $a = Cherry $b = Banana $c = Grapes
EXTR_PREFIX_IF_EXISTS: $copy4_a = Cherry $b = Banana $c = Grapes
EXTR_REFS: $a = Apple $b = Banana $c = Grapes


PHP extract() function is used to import variables into the local symbol table from an array.

PHP extract() function parameters:

1- Array (Required)
2- Rules for extracting variables (Optional):
– This parameter tells how to treat with invalid and colliding names.
– Possible parametric values:
EXTR_OVERWRITE – Default. If names are collide existing variable is overwritten as shown in above example $a = 'Carrot' is overwrite by $a = 'Apple'.
EXTR_SKIP – If names are collide existing variable is not overwritten as shown in above example.
EXTR_PREFIX_SAME – If names are collide existing variable will be given a prefix as shown in above example.
EXTR_PREFIX_ALL – All variable names will be given a prefix as shown in above example.
EXTR_PREFIX_INVALID – Only invalid or numeric variable names will be given a prefix as shown in above example.
EXTR_IF_EXISTS – Only overwrite existing variables in the current symbol table, otherwise do nothing as shown in above example.
EXTR_PREFIX_IF_EXISTS – Only add prefix to variables if the same variable exists in the current symbol table as shown in above example.
EXTR_REFS – Extracts variables as references. The imported variables are still referencing the values of the array parameter as shown in above example.
3- Prefix (Optional):
– This parameter specifies the prefix. The prefix is automatically separated from the array key by an underscore character as shown in above example.
– It is required with following extract rules:

PHP array_search() Function Example

PHP Script:

$array = array(0 => 'book', 1 => 'cup', 'index'=>'text', 2 => 'spoon',3 => 'fork');
$key = array_search('text', $array);
echo "$key\n";
$key = array_search('cup', $array);
echo "$key\n";




PHP array_search() function is used for searching an array for a given value and returns the corresponding array key if the given value is found.

PHP array_search() function parameters:

1- mixed: (required): The value to be searched.
2- Array (required): The array in which value is searched.
3- Bool(optional), Default False: This Boolean flag when set to true, then this function tires to check the type of the value begin searched and checks if both items references to the same  object.

PHP array_diff() Function Example

PHP Script:



$arr3 = array("CE","EE","TE");
$arr4 = array("EE","BE","ME");
$arr5 = array("IM","TE","ME");



[b] => Lion
[c] => Tiger
[d] => Cat
[0] => CE


PHP array_diff() function is used to compare the values of two or more arrays and returns the differences as an array which contains the elements from an Array1 that are not present in other arrays.

PHP array_diff() function parameters:

1- Array1 (required)
2- Array2 (required)
3- Array3 (optional)