-2

I have this text-array

$text = array( "T02" => array( 'EN' => 'English text for T02'), 
           "T03" => array( 'FR' => 'Texte anglais pour T03'));
$language = "FR";
$testtext = "Translated text: {#text['T03'][$language]}";   

I need the # here because I get this text from a database through a function.

So I need to replace de # with a $

// This will work, but I need 'T03' and $language to be variable:

$output = str_replace("{#text['T03'][$language]}","{$text['T03'][$language]}",$testtext); 
echo $output;

// This won't work:
$output = str_replace("#text","$text",$testtext);
echo $output;   // I get ==> Translated text: {Array['T03'][FR]} 

How can I replace "{#text['..var1..'][..var2..]}" ==> {$text['..var1..'][..var2..]} ?

choroba
  • 231,213
  • 25
  • 204
  • 289
Eduard
  • 25
  • 4
  • This is PHP code, right? – Thorben Jun 05 '14 at 16:38
  • I replaced the `bash` tag with `php`. – choroba Jun 05 '14 at 16:39
  • It won't work that way because you're using double quotes, so `$testtext` is not what you create the image of in your example code. See http://php.net/string and http://php.net/var_dump. If you're looking for a search and replace function with strings, take a look in the string function section: http://php.net/strings – hakre Jun 05 '14 at 18:41
  • possible duplicate of [Best way to substitute variables in plain text using PHP](http://stackoverflow.com/questions/283222/best-way-to-substitute-variables-in-plain-text-using-php) – hakre Jun 05 '14 at 18:59

1 Answers1

0

Use preg_replace

<?php
$text = "{#text['..var1..'][..var2..]}";
$pattern = '/{#(.*)/';
echo preg_replace($pattern, '{\$$1', $text);

@Eduard, you have no idea what you are asking for. Please use this. It's still bad but at least won't blow you app up.

<?php
$text = array( "T02" => array( 'EN' => 'English text for T02'), 
           "T03" => array( 'FR' => 'Texte anglais pour T03'));
$language = "'FR'";
$testtext = "Translated text: {#text['T03'][$language]}"; 
$pattern = '/{#(?P<varName>\S+)\[\'(?P<someKey>\S+)\'\]\[\'(?P<langKey>\S+)\'\]}/';

$callback = function($match)use($text){
    return $text[$match['someKey']][$match['langKey']];
};
echo preg_replace_callback($pattern,$callback, $testtext);

For special request, all sanity ends here. Enter at your own risk!

<?php
$text = array( "T02" => array( 'EN' => 'English text for T02'), 
           "T03" => array( 'FR' => 'Texte anglais pour T03'));
$language = "'FR'";
$testtext = "Translated text: {#text['T03'][$language]}"; 
$pattern = '/{#(.*?)}/';


echo eval('return "'.preg_replace($pattern, '{\$$1}', $testtext).'";');
mleko
  • 11,650
  • 6
  • 50
  • 71
  • This doen't help. I get "$text['T03'][FR]}", but it doesn't activate the array. – Eduard Jun 05 '14 at 17:52
  • Of course it doesn't. It's a string, why it should become array? – mleko Jun 05 '14 at 17:54
  • $text = array( "T02" => array( 'EN' => 'English text for T02'), "T03" => array( 'FR' => 'Texte anglais pour T03')); is a array – Eduard Jun 05 '14 at 18:00
  • http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem What you really want to do? – mleko Jun 05 '14 at 18:05
  • $language = "EN"; $testtext = "Translated text: {#text['T02'][$language]}"; => Should give: 'Translated text: English text for T02') $language = "FR"; $testtext = "Translated text: {#text['T03'][$language]}"; =? Should give: 'Translated text: Texte anglais pour T03') – Eduard Jun 05 '14 at 18:12
  • $output = str_replace("{#text['T03'][$language]}","{$text['T03'][$language]}",$testtext); works, but it needs to be variable – Eduard Jun 05 '14 at 18:13
  • @mleko: Because of the double quoted string `$language` will be already substituted and won't work any longer (as substuted with the contenst of `$language` which is not `var_export((string) $language, true)` but just `(string) $language`. That is most likely due to a confusion by the OP about string fundamentals in PHP with double-quoted strings. Can happen, just needs education ;) – hakre Jun 05 '14 at 18:43
  • @hakre It will, my eyes are bleeding but this code works. You can check – mleko Jun 05 '14 at 18:47
  • Yes, but only with constant to string conversion warnings. Why not before assigning `$testtest` do `$language = '$language';` while keeping a backup of the original value ^^ :D - Or directly: `$language = var_export((string) $language, TRUE);` – hakre Jun 05 '14 at 18:50
  • @hakre Thats why I used `$language = "'FR'";` in my code. There is eval in code and you care about warning? May God forgive me that eval. – mleko Jun 05 '14 at 18:54
  • Ah, I see. Yeah, may god forgive the eval. It perhaps will kill the OPs site and server one day :) Let's cross fingers it's not the last server in the world then :) – hakre Jun 05 '14 at 18:55
  • @hakre if he will just copy paste that blasphemy I wrote I'm quite sure his server will be killed. I doubt it will be God, I bet on some bored 11 year old. – mleko Jun 05 '14 at 18:58
  • @mleko: Just a hint: Better not answer. Esp. as OP begged for code in comments. Just kill it, you find better questions to deal with I'm sure. – hakre Jun 05 '14 at 19:00
  • @mleko: Thank you! It's working now for my purpose. I don't see why this code is blasphemy? – Eduard Jun 06 '14 at 13:20
  • this become way to long. Lets move to http://chat.stackoverflow.com/rooms/55201/questions-24065389 – mleko Jun 06 '14 at 14:10
  • I understand. I don't have 20 reputation, so I can't answer you in the chatroom. Thanks anyway. – Eduard Jun 06 '14 at 14:30