D
D
Danil2020-03-14 19:16:34
linux
Danil, 2020-03-14 19:16:34

How to create custom macros for text in PHP?

Hello,

I have a question how to create my own macros for text in PHP so that they can be fully managed, etc.?
Regular str_replace(); not suitable because I still need the ability to escape the macro (with the "\" character), and I also need the ability to multi-level replacement.

<?php

$macros = [
    "{user}" => "Вася",
    "{date}" => date("d.m.y"),
    "{user_id}" => 1,
    "{rand:(min):(max)}" => rand($min, $max)
];
$text = "Привет {user}, сегодня {date}. Ваш ID {user_id}. Ваше рандомное число -- {rand:0:10}";
echo replace($text);


Result: Hello Vasya, today is 03/14/2020. Your ID 1. Your random number is 5

Answer the question

In order to leave comments, you need to log in

7 answer(s)
S
Sergey, 2019-08-13
@Weidmann

You can, this technology is called nested virtualization.
Works quite successfully in KVM and Hyper-V.
In order to use with kvm, support from the host system is required, 99% that the host will not enable it.

A
athacker, 2019-08-09
@athacker

https://www.linuxtechi.com/enable-nested-virtualiz...

R
Ruslan Fedoseev, 2019-08-10
@martin74ua

Ask this question to the technical support of the hosting. The possibility of nested virtualization depends on the settings of the host hypervisor. On commercial vds hosting, this will most often not be possible. You will simply be offered to take a server and configure and install anything on it.
If I were you, I would look at soyoustart.com or even kimsufi.com

V
Vladimir Zp, 2019-08-10
@Zeroxzed

I ran virtual machines in proxmox installed in hyper-v.

V
vampire333, 2019-08-12
@vampire333

To get rid of problems, I recommend renting several inexpensive vps.
You can search here: https://lowendbox.com/

A
Anton Shamanov, 2020-03-15
@Oneclassic

if you need something more complicated than placeholders, then look towards preg_replace_callback_array, otherwise see ffosters answer

B
bkosun, 2020-03-14
@bkosun

Use the strtr function:

$str = strtr('Hello {name}!', [
    '{name}' => 'World',
]);

https://www.php.net/manual/en/function.strtr.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question