Jump to content

getting a part of the content of a page


Alkimuz

Recommended Posts

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!
 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/276569-getting-a-part-of-the-content-of-a-page/
Share on other sites

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 :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.