Alkimuz Posted April 5, 2013 Share Posted April 5, 2013 my end goal is to show an invoice with data of the customer, therefor i made a templatefile with pure html and within a function, i get the content of the templatefile and replace specific words in the templatefile with the data of the customer. so far so good. but now, i have a repeatitive part of the templatefile, the one with the bought items.. my idea is to substract that specific part out of the variable with the content of the templatefile, duplicate and fill it with a while loop and put it back in the variable, replacing the original.. (hopefully your still with me?) this works: $url = $_SESSION['dir'].'php/invoice.php'; $invoice = file_get_contents($url); //replace data $replacement = array("name", "prefix", "ordernumber", "orderdate", "address", "zipcode", "place", "country", "ordername", "orderaddress", "orderzipcode", "orderplace", "ordercountry"); foreach ($replacement as $word){ $invoice = str_replace('{$'.$word.'}', $data[$word], $invoice); } so the whole invoice code is in $invoice and the data of the customer is allready in it now time for the next step: the substraction of the specific part, which is between these tags: <!-- repeat --> <!-- /repeat --> i was thinking of: preg_match("/<!-- repeat -->(.*)<!-- \/repeat -->/", $invoice, $invoicepart); but that does not work.. is that because the syntax is wrong or because the part to select excists of a lot of html code? after that, i was think of: $itemresult = item_query(); while ($itemrow = mysql_fetch_array($itemresult)){ //replace item data $temporalpart = $invoicepart; $replacement = array("itemname", "itemamount", "itemprice", "itempricetotal"); foreach ($replacement as $word){ $temporalpart= str_replace('{$'.$word.'}', $itemrow[$word], $temporalpart); } $newinvoicepart.= $temporalpart } $invoice = str_replace("$invoicepart", "$newinvoicepart", "$invoice"); would that be the right way to go further? many thanks for helping me! Quote Link to comment Share on other sites More sharing options...
davidannis Posted April 5, 2013 Share Posted April 5, 2013 I think that should work. Did you try it? Quote Link to comment Share on other sites More sharing options...
exeTrix Posted April 5, 2013 Share Posted April 5, 2013 The regex won't work because you're using special characters as literals. This is purely from memory but you'll need to escape < and ! characters. Also, str_replace will accept arrays as parameters. So as long as you make sure they're in the correct order: $find = array( 'foo', 'bar' ); $replacements = array( 'Yo', 'Hoe'); $str = 'foo bar'; echo str_replace( $find, $replacements, $str ); //this is wrong btw str_replace('{$'.$word.'}', $itemrow[$word], $temporalpart); //needs to be this if you're going down that route str_replace($word, $itemrow[$word], $temporalpart); Hope that helps 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.