ndtex Posted March 26, 2008 Share Posted March 26, 2008 I've been coding a web application that will allow users to fill in a form and have it output into an editable word document. To do this, I have a saved .rft file on the server, and make several different queries to a mySQL database depending on the form and have str_replace replace the "variables" that I have in my template. I've noticed a couple of odd bugs in the output while debugging this script and I am completely stumped. First, for some reason some words are simply not being replaced. This happens in two instances: $dataText = str_replace ('$system_type',$system_type, $dataText); $dataText = str_replace ('$customer',$customer_name, $dataText); For some reason there is always one instance where each of these will not replace correctly, however, it will replace every other instance. Another odd thing to note here is that for the $system_type, the failed replacement happens in the beginning of the document and the $customer replacement fails at the end. I also have several other scattered instances where I'm trying to replace only one value and it doesn't work at all. For some of these cases, I was able to change the variable name and it worked, but in other cases it still fails. It's all very strange because about 3/4ths of the str_replace calls work (and I've double checked spelling and variable names for them all), leaving me with a really messed up output at the end. Is there something about str_replace that I'm not realizing? Any help would be appreciated. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/98044-odd-str_replace-bugs/ Share on other sites More sharing options...
Demonic Posted March 26, 2008 Share Posted March 26, 2008 php variables don't work in single quotes, try double quotes. Quote Link to comment https://forums.phpfreaks.com/topic/98044-odd-str_replace-bugs/#findComment-501645 Share on other sites More sharing options...
wildteen88 Posted March 26, 2008 Share Posted March 26, 2008 php variables don't work in single quotes, try double quotes. Or remove the quotes completely. Quote Link to comment https://forums.phpfreaks.com/topic/98044-odd-str_replace-bugs/#findComment-501651 Share on other sites More sharing options...
ndtex Posted March 26, 2008 Author Share Posted March 26, 2008 The first variable in each of those calls isn't a PHP variable it is a string in my .rtf template. So I'd have something like: This is normal text, but $this_value should change. Then I would try to replace the string "$this_value" in the document with a PHP variable. Quote Link to comment https://forums.phpfreaks.com/topic/98044-odd-str_replace-bugs/#findComment-501658 Share on other sites More sharing options...
cooldude832 Posted March 26, 2008 Share Posted March 26, 2008 php variables don't work in single quotes, try double quotes. read very specifically on quotation use of variables in connotation because I believe you are misinterpreting it. Varying quotations provide different versions of a variable or the literal string "$var" depending on the connotation and quotation style and {} use However in my studies of php I have found this is the safest way to connoation vars <?php $q = "Select this from `".$table."` where email = '".$email."'"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/98044-odd-str_replace-bugs/#findComment-501660 Share on other sites More sharing options...
ndtex Posted March 26, 2008 Author Share Posted March 26, 2008 Just to give another example of what I mean (because I'm not sure I have explained the issue too well). Here is another example: The following appears in my document called "Template.rtf": Tasks Performed for a $system_type $provider_name_long will perform the following tasks for the $system_type system: Here I have 2 cases of str_replace needed and the following is in my PHP script: $dataText = str_replace ('$system_type',$system_type, $dataText); $dataText = str_replace ('$prodiver_name_long', $provider_name_long, $dataText); The resulting Word document I get at the end has this: Tasks Performed for a $system_type Shared Technologies will perform the following tasks for the Option 11C Cabinet system So for some reason, one of the variables refuses to become replaced. This happens in several places throughout my document as well. I hope this clears up my issue. Quote Link to comment https://forums.phpfreaks.com/topic/98044-odd-str_replace-bugs/#findComment-501669 Share on other sites More sharing options...
Jeremysr Posted March 26, 2008 Share Posted March 26, 2008 I'm not sure what would cause that, but I would try using a character other than $ in front of variables you want to replace, just in case that's what's causing str_replace to mess up (even though it probably shouldn't be.) Quote Link to comment https://forums.phpfreaks.com/topic/98044-odd-str_replace-bugs/#findComment-501680 Share on other sites More sharing options...
ndtex Posted March 26, 2008 Author Share Posted March 26, 2008 I'm not sure what would cause that, but I would try using a character other than $ in front of variables you want to replace, just in case that's what's causing str_replace to mess up (even though it probably shouldn't be.) That is one avenue I've tried. For instance doing %system_type and even %system_type%, but to no avail. The only time I got something like that to work was I had one variable called $env_req, and I changed it to $env and it worked. No other places in the code has this work though. Needless to say I'm completely stumped. ??? Quote Link to comment https://forums.phpfreaks.com/topic/98044-odd-str_replace-bugs/#findComment-501684 Share on other sites More sharing options...
Jeremysr Posted March 26, 2008 Share Posted March 26, 2008 Maybe you could try making your own str_replace function, using strpos() to find the substring and substr() to replace it. (Concatenate the substring to the left of the string you're replacing, with the string you want to replace it with, and then with substring to the right of the substring you're replacing.) Quote Link to comment https://forums.phpfreaks.com/topic/98044-odd-str_replace-bugs/#findComment-501704 Share on other sites More sharing options...
kenrbnsn Posted March 26, 2008 Share Posted March 26, 2008 Here's a quick script I just wrote to test your example: <?php $inp = file('test.rtf'); $system_type = 'Option 11C Cabinet'; $provider_name_long = 'Shared Technologies'; $repl_arr = array('$provider_name_long','$system_type'); $replaced_arr = array($provider_name_long,$system_type); $out = array(); foreach($inp as $line) $out[] = str_replace($repl_arr,$replaced_arr,$line); echo '<pre>' . print_r($out,true) . '</pre>'; $fp = fopen('test_out.rtf','w'); fwrite($fp,implode('',$out)); fclose($fp); ?> Here's the input: Tasks Performed for a $system_type $provider_name_long will perform the following tasks for the $system_type system: and the output: Tasks Performed for a Option 11C Cabinet Shared Technologies will perform the following tasks for the Option 11C Cabinet system: See if your code looks anything like mine. Ken Quote Link to comment https://forums.phpfreaks.com/topic/98044-odd-str_replace-bugs/#findComment-501722 Share on other sites More sharing options...
ndtex Posted March 27, 2008 Author Share Posted March 27, 2008 Here's a quick script I just wrote to test your example: <?php $inp = file('test.rtf'); $system_type = 'Option 11C Cabinet'; $provider_name_long = 'Shared Technologies'; $repl_arr = array('$provider_name_long','$system_type'); $replaced_arr = array($provider_name_long,$system_type); $out = array(); foreach($inp as $line) $out[] = str_replace($repl_arr,$replaced_arr,$line); echo '<pre>' . print_r($out,true) . '</pre>'; $fp = fopen('test_out.rtf','w'); fwrite($fp,implode('',$out)); fclose($fp); ?> First off, my code looks very little like that. In pseudo-code I was doing this: Check from inputs if input == something, then grab proper text from mySQL database and use str_replace else, use text needed or set template variable to NULL (if not needed) Once all queries are done: use str_replace for all variables Which means at the end of my code I have about 20 or so str_replace functions and even more within the mySQL queries. Would feeding that many str_replace commands cause my bugs? Also I want to make sure I fully understand your code: Is the foreach loop actually going line-by-line and building a new output string the same way? Why did you use implode at the end? When you use arrays, as long as each element matches correctly it doesn't matter which order they appear in the document correct? It seems you are trying to take in the template .rtf file and then building a new string from that (which honestly, sounds like a much better way than what I am trying as you can see above, I'm using the same variable for the .rtf replacements). My best guess is implode is taking out white space (another cosmetic issue I'm having). Thank you all for your help. I'm happy I can at least have a way to attack this issue. Quote Link to comment https://forums.phpfreaks.com/topic/98044-odd-str_replace-bugs/#findComment-501830 Share on other sites More sharing options...
ndtex Posted March 27, 2008 Author Share Posted March 27, 2008 Well I tried modifying my code to the above and now I'm getting a HTTP 500 error. I'm using IIS and have turned off friendly HTTP errors in IE options as well. With that unchecked, I get a blank screen. My initial guess was that IIS didn't like file() so I tried fopen() again, and that still didn't work. I've checked the event viewer and have found nothing in the logs as well. Any other ideas of where to look? :-\ Quote Link to comment https://forums.phpfreaks.com/topic/98044-odd-str_replace-bugs/#findComment-502317 Share on other sites More sharing options...
ndtex Posted March 27, 2008 Author Share Posted March 27, 2008 Ignore the above it all works!!! HTTP 500 was caused by parse errors IIS was refusing to show (tested on a Linux server and it showed them). It also appears going through the file line by line ensures str_replace works correctly. Well...mostly. I have my arrays ordered incorrectly, but that is just due so some sloppy coding by me. The important thing is that every value I am wanting to be replaced is being replaced. Now it is just a matter of lining up my arrays correctly. Thanks so much for all the help. Quote Link to comment https://forums.phpfreaks.com/topic/98044-odd-str_replace-bugs/#findComment-502355 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.