Skip to content

stemar/php-recursive-replace

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP recursive replace

Recursively replace array with placeholders in PHP.

Goal

We need a recursive replacement of values within the same array.

Instead of repeating values throughout a JSON file or a PHP Array, use placeholders to fill in repeatable portions of values.

Example:

{
    "home": "/home/vagrant",
    "app": "php_example_app",
    "app_version": "1.3.0",
    "app_path": "{{home}}/{{app}}",
    "app_version_path": "{{app_path}}/{{app_version}}",
    "webroot_path": "{{app_version_path}}/www",
    "config_path": "{{app_version_path}}/app/config"
}

Instead of:

{
    "home": "/home/vagrant",
    "app": "php_example_app",
    "app_version": "1.3.0",
    "app_path": "/home/vagrant/php_example_app",
    "app_version_path": "/home/vagrant/php_example_app/1.3.0",
    "webroot_path": "/home/vagrant/php_example_app/1.3.0/www",
    "config_path": "/home/vagrant/php_example_app/1.3.0/app/config"
}

Installation

git clone https://github.com/stemar/php-recursive-replace.git
cd php-recursive-replace

Test

One local file

wget -O phpunit.phar https://phar.phpunit.de/phpunit-8.phar
php phpunit.phar ReplaceRecursiveTest.php

With Composer

curl -sS https://getcomposer.org/installer | php
php composer.phar require phpunit/phpunit
vendor/bin/phpunit ReplaceRecursiveTest.php

Usage

Copy the replace() function from replace_recursive.php anywhere in your code.

Examples

Try:

<?php
$array = [
    "home" => "/home/vagrant",
    "app" => "php_example_app",
    "app_version" => "1.3.0",
    "app_path" => "{{home}}/{{app}}",
    "app_version_path" => "{{app_path}}/{{app_version}}",
    "webroot_path" => "{{app_version_path}}/www",
    "config_path" => "{{app_version_path}}/app/config"
];
$replaced_array = replace($array, $array);
echo replace("App version path: {{app_version_path}}", $replaced_array);

Result:

App version path: /home/vagrant/php_example_app/1.3.0

Try:

$json = <<<JSON
{
    "home": "/home/vagrant",
    "app": "php_example_app",
    "app_version": "1.3.0",
    "app_path": "{{home}}/{{app}}",
    "app_version_path": "{{app_path}}/{{app_version}}",
    "webroot_path": "{{app_version_path}}/www",
    "config_path": "{{app_version_path}}/app/config"
}
JSON;
$array = json_decode($json, TRUE);
$replaced_array = replace($array, $array);
echo replace("Config path: {{config_path}}", $replaced_array);

Result:

Config path: /home/vagrant/php_example_app/1.3.0/app/config

Try with a symbol placeholder:

$json = <<<JSON
{
    "home": "/home/vagrant",
    "app": "php_example_app",
    "app_version": "1.3.0",
    "app_path": ":home/:app",
    "app_version_path": ":app_path/:app_version",
    "webroot_path": ":app_version_path/www",
    "config_path": ":app_version_path/app/config"
}
JSON;
$array = json_decode($json, TRUE);
$replaced_array = replace($array, $array, '/:(\w+)/');
echo replace("Config path: :config_path", $replaced_array, '/:(\w+)/');

Result:

Config path: /home/vagrant/php_example_app/1.3.0/app/config

About

Recursively replace array with placeholders in PHP.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages