lawless Posted September 13, 2014 Share Posted September 13, 2014 Hello there I'm looking for an solution to an very specific problem. In order to make my template system working, I need to replace certain strings with different values. For example %%title%% will become $value['title']. Now when trying this manually with strtr() or str_replace() everything works fine. This is kinda much to code, so I'm running an array through the strtr() function. The problem is, that my array does not take the value : $value['title'] (string) $array1['%%title%%'] = "{$value['title']}"; // same as $array1 = array( "%%title%%" => "{$value['title']}"); I tried it with brackets, without, with escaping (backslash), with reference and I don't know why this does not work. The $value variable is another array which contains entries from the database (already running inside an foreach loop where this code is situated at). Without the array, I can echo out the content without problems. After I add this value to my array, var_dump() gives me: Array ( [%%title%%] => ) It SHOULD give me: Array ( [%%title%%] => $value['title'] ) That is what i need to run the string replacement function. And yes, I tried it with different keys. This key does work properly as intended (function does take it as normal string). So the problem only can be situated at why my array does not take this string. Can I mark this as string somehow I do not know of? Quote Link to comment Share on other sites More sharing options...
jcbones Posted September 13, 2014 Share Posted September 13, 2014 What is your var_dump of $value? Quote Link to comment Share on other sites More sharing options...
kicken Posted September 13, 2014 Share Posted September 13, 2014 It SHOULD give me: Just to clarify, do you want it to be the value contained in the $value['title'] variable, or do you want it to be literally '$value['title']'? If the former, var_dump $value to make sure it contains that key and that it has a value to start with. If the later then you need to either escape the $ or use single quotes around the string. $array1['%%title%%'] = "\$value['title']"; Quote Link to comment Share on other sites More sharing options...
lawless Posted September 14, 2014 Author Share Posted September 14, 2014 Well I had the insane idea, that I must - Create an array which contains the wildcards as KEYS and the variable (as string) as VALUES - run through an foreach loop to replace those (string) VALUES with the correct VALUES - run through another foreach loop to replace the actual template wildcards with the (now) correct VALUES This was one logical approach I do not wish anyone to think about For the solution I did the following: - Create an array with the acutal VALUES that should be entered in the string - run the strtr() function This is so much easier then whatever twisted thought I had in mind before. The problems with the first approach were (a) that is was too complicated and (b) catching the string to replace with the value. For that I would have had to run another foreach loop to rush through the array and replace the placeholder values with the correct values anyways. So why not directly inserting the correct value at the first time Thank you very much for your help. I guess sometimes you've gotta take a small break from overthinking an solution. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.