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)