String will replace PHP with a return function

In javascript, you can define a return function when you do string replacements:

function miniTemplate(text, data) { return text.replace(/\{\{(.+?)\}\}/g, function(a, b) { return typeof data[b] !== 'undefined' ? data[b] : ''; }); } 

These few lines of code allow me to create a very neat template system. The regular expression matches all the {{something}} lines inside the text variable, and the return function matches if something is inside the object data, and if so, it replaces it.

So,

 text = "Hello {{var1}}, nice to meet {{var2}}"; data = { var1: "World", var2: "You" } //result => "Hello World, nice to meet You" 

I am trying to replicate this functionality - this is PHP, but the only solution that comes to my mind is to use two cicles that parse each element of the data array, and the second inside the first that looks for the string inside Text.

Is there a cleaner way in php?

+5
source share
7 answers

You can use preg_replace_callback in the same way as in similar JavaScript (pass the $data array to preg_replace_callback using the uses keyword):

 function miniTemplate($text, $data) { return preg_replace_callback('~\{\{(.*?)}}~', function ($m) use ($data) { return isset($data[$m[1]]) ? $data[$m[1]] : $m[0]; }, $text); } $text = "Hello {{var1}}, nice to meet {{var2}}"; $data = array("var1" => "World", "var2"=> "You"); echo miniTemplate($text, $data); // => Hello World, nice to meet You at {{var3}} 

Watch the IDEONE demo

If there is no value in $data , the template string will be returned, since we will first check if it is present with isset($data[$m[1]]) .

+3
source

Yes, in PHP there is a preg_replace_callback() function that you can pass to functions to handle the replacement:

 $result = preg_replace_callback('/\{\{(.+?)\}\}/', 'do_replacement', $subject); function do_replacement($groups) { // $groups[0] holds the entire regex match // $groups[1] holds the match for capturing group 1 return ''; // actual program logic here } 
+3
source

Try this code. It will definitely help you.

 <?php $text = "Hello {{var1}}, nice to meet {{var2}}"; $data = array("var1"=>"World","var2"=>"You"); foreach($data as $key => $value){ $text = str_replace('{{'.$key.'}}', $value, $text); } echo $text; ?> 
+2
source

You can use preg_replace_callback for this.

Code example:

 $result = preg_replace_callback('/\{\{(.+?)\}\}/', 'replacementFunction', $subject); function replacementFunction($groups) { //code login here return "value"; } 
+1
source

I think we do not need to think about complex things about this need. If we try to keep it simple, you can use the sprintf function for php to format the text, you can try the code below;

 <?PHP $format = "Hello %s, nice to meet %s"; $jsonData = "{\"var1\": \"World\", \"var2\": \"You\" }"; $data = json_decode($jsonData); $result = sprintf($format,$data->var1,$data->var2); echo $result; ?> 

A working example is here https://ideone.com/AGNZen

+1
source
 <?php $data = array("var1" => "World", "var2" => "You"); echo "Hello {$data['var1']}, nice to meet {$data['var2']}"; ?> 

refactoring ...

+1
source

A quick fix might be

 <?php function ReplaceWord($find,$replace,$srcstring) { retrun str_replace($find,$replace,$srcstring); } echo ReplaceWord("WORLD","Peter","Hello world!"); ?> 
-1
source

Source: https://habr.com/ru/post/1241592/


All Articles