Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

preg_replace between tags...


Supertex

Recommended Posts

I have the 'quantity discount' order total module installed, and am using the printable customer PDF invoice.  But when the order includes a quantity discount, there's a line of code printed to the PDF.

 

I looked around and found something, and adapted it to what I thought would work:

$startPoint = '<span class="orderEdit">';
$endPoint = '</span>';	
$order->totals[$i]['text'] = preg_replace('#('.preg_quote($startPoint).')(.*)('.preg_quote($endPoint).')#si', 'Quantity Discount', $order->totals[$i]['text']);

But I'm still getting :

 

<span class="orderEdit"><a href="javascript:alert('Your Current Quantity Discount\n\n 104 items:$702.12 off\n\n\nTotal Discount: $702.12');">Quantity Discount</a></span>

 

In the order totals.  

 

Can someone point me in the right direction?

 

 

 
Link to comment
Share on other sites

I suspect the preg_quote() call may messing you up by escaping the <>'s. Try it without those calls.

 

You want to replace the span and anchor tags with just some fixed text? BTW, if those are the only tags showing up, you might want to try strip_tags() instead.

Link to comment
Share on other sites

@@MrPhil

 

Thanks for the reply, and my apologies for my late reply...had this typed out on my laptop and never hit "Post", apparently.  

 

Yes, I want to replace the tags with fixed text.  On the pdf, that line should only be "Quantity Discount".  There is a span and an anchor tagset.

 

I'll look into strip_tags, then.  Will that remove the tags and the content between them?

Link to comment
Share on other sites

It is supposed to remove only the tags themselves, and not child content (such as text "Quantity Discount" in your example). If you also want to get rid of the child content, I don't thing strip_tags() will do the entire job for you. At best, you would have to take action to remove or change the remaining child content (text).

 

Who hasn't (at least once) sat there and wondered why the computer wasn't doing anything, only to remember that you haven't pressed a real or virtual button!

Link to comment
Share on other sites

@@MrPhil

 

Well, I tried it, but I think I'm doing sumping wong. =/

 

Didn't work - the tags still print on the pdf: 

$order->totals[$i]['text'] = strip_tags($order->totals[$i]['text']);

I hate to show my ignorance to this degree, but is the "->" and the bracketry throwing this off?

 

 

mmm...to lessen my ignorance...

 

If this is a variable: $test

What is this: $test->name[$i]['stuff']

 

an "expression"?

Link to comment
Share on other sites

Checked and double checked.  Here's the snippet in context:

for ($i = 0, $n = sizeof($order->totals); $i < $n; $i++) {
	$pdf->SetFont(PDF_INV_CORE_FONT,'B',8);
	$pdf->SetY($Y_Table_Position + 3);
	$pdf->SetX(9);
	$order->totals[$i]['text'] = preg_replace( $search_for, $change_to, $order->totals[$i]['text']);
	$order->totals[$i]['text'] = strip_tags($order->totals[$i]['text']);
	//$startPoint = '<span class="orderEdit">';
	//$endPoint = '</span>';	
	//$order->totals[$i]['text'] = preg_replace('#('.preg_quote($startPoint).')([\s\S]*)('.preg_quote($endPoint).')#si', '', $order->totals[$i]['text']);
	//$order->totals[$i]['text'] = preg_replace('/</', '', $order->totals[$i]['text']);
	$pdf->MultiCell(177,5,$order->totals[$i]['title'] . '  ' ,0,'R');
	$pdf->SetY($Y_Table_Position + 3);
	$pdf->SetX(186);
	$pdf->MultiCell(15,5,$order->totals[$i]['text'],0,'R');
	$Y_Table_Position += 5;
}

And here's what the DB is pulling, that the code should act on, leaving only the bold text:

<span class="orderEdit"><a href="javascript:alert('Your Current Quantity Discount\n\n 104 items:$702.12 off\n\n\nTotal Discount: $702.12');">Quantity Discount</a></span>

Link to comment
Share on other sites

@@Supertex I'm not sure preg_quote is needed for this. See if this works:

 $order->totals[$i]['text'] = preg_replace('#' .$startPoint . '(.*)' . $endPoint . '#i', 'Quantity Discount', $order->totals[$i]['text']);

Support Links:

For Hire: Contact me for anything you need help with for your shop: upgrading, hosting, repairs, code written, etc.

Get the latest versions of my addons

Recommended SEO Addons

Link to comment
Share on other sites

strip_tags is the function you need.  Make a new page, forget all this awful PDF code for now:

 

 

<?php
$order->totals[$i]['text'] = '<span class="orderEdit"><a href="javascript:alert(\'Your Current Quantity Discount\n\n 104 items:$702.12 off\n\n\nTotal Discount: $702.12\');">Quantity Discount</a></span>';
 
echo $order->totals[$i]['text'];
echo '<br><br>';
echo strip_tags($order->totals[$i]['text']);
Open the page in your browser and what do you see?
Link to comment
Share on other sites

What is line 851 in there for? Are you trying to remove tags at that point, or doing something else? What exactly is the content of $orders->totals[$i][$text] before line 852 is executed?

 

1) 851 is not my code; it's part of the original file.The $search_for and $change_to are defined way up at line 61 as:  

  $search_for = array( '/'.$currency_sym.'/', '/Â/', '(<[a-z/]*>)'); 
  $change_to = array( ' ', '', '');

2) I picked line 852 to remove tags, because I assumed since regex worked there in the original code, it was the right place to put my code.  Perhaps my assumption that it actually worked there was wrong.

 

3) This is the content as it exists in the DB, before any modification.  It's also exactly the same on the PDF, as near as I can tell, because it's so long that it spills over other text and is illegible in some areas.

<span class="orderEdit"><a href="javascript:alert('Your Current Quantity Discount\n\n 104 items:$702.12 off\n\n\nTotal Discount: $702.12');">Quantity Discount</a></span>

And here is a jpg of the output pdf:

 

post-292008-0-38963900-1423674809_thumb.jpg

 

 

 

@@Supertex I'm not sure preg_quote is needed for this. See if this works:

 $order->totals[$i]['text'] = preg_replace('#' .$startPoint . '(.*)' . $endPoint . '#i', 'Quantity Discount', $order->totals[$i]['text']);

 

Tried this, and did not work either.

 

 

strip_tags is the function you need.  Make a new page, forget all this awful PDF code for now:

 

 

<?php
$order->totals[$i]['text'] = '<span class="orderEdit"><a href="javascript:alert(\'Your Current Quantity Discount\n\n 104 items:$702.12 off\n\n\nTotal Discount: $702.12\');">Quantity Discount</a></span>';
 
echo $order->totals[$i]['text'];
echo '<br><br>';
echo strip_tags($order->totals[$i]['text']);
Open the page in your browser and what do you see?

 

 

Exactly what you expect.  A "Quantity Discount" hyperlink with the same behavior as the checkout confirmation page, and two lines below that, plain text "Quantity Discount".

 

--------------------------------------------------------------------------------------------------------------------------------------

I have no doubt that strip_tags should do EXACTLY what you've both told me it would.  I did read the documentation for it and I agree...this should work.  I don't understand why in this case, it doesn't.  Perhaps line 851 doesn't work either, but I've got no special chars to replace so it's never presented?

 

I'll attach the file, in case you'd like to see it in its entirety.

pdfinvoice.php

Link to comment
Share on other sites

I tried the code I posted here and it worked. I replaced the $order variable with just one I created but that wouldn't make a difference. Are you sure the $startPoint and $endpoint variables can be seen where you added the code? What happens if you try this is it own file?

	$startPoint = '<span class="orderEdit">';
	$endPoint = '</span>';		
 $text = preg_replace('#' .$startPoint . '(.*)' . $endPoint . '#i', 'Quantity Discount', $text);	
 echo $text;

Support Links:

For Hire: Contact me for anything you need help with for your shop: upgrading, hosting, repairs, code written, etc.

Get the latest versions of my addons

Recommended SEO Addons

Link to comment
Share on other sites

 

I tried the code I posted here and it worked. I replaced the $order variable with just one I created but that wouldn't make a difference. Are you sure the $startPoint and $endpoint variables can be seen where you added the code? What happens if you try this is it own file?

	$startPoint = '<span class="orderEdit">';
	$endPoint = '</span>';		
 $text = preg_replace('#' .$startPoint . '(.*)' . $endPoint . '#i', 'Quantity Discount', $text);	
 echo $text;

 

Initially, I put $startPoint and $endPoint up where the find and replace strings for line 851 are found.  I added them just below it.  That didn't work, so I put them inside the function, just above line 851, which of course didn't work.  I tried a few other things...all still in the attached file, but commented out.

 

When I put this in its own file:

$text='<span class="orderEdit"><a href="javascript:alert(\'Your Current Quantity Discount\n\n 104 items:$702.12 off\n\n\nTotal Discount: $702.12\');">Quantity Discount</a></span';
echo $text  .  '<br><br>';
$startPoint = '<span class="orderEdit">';
$endPoint = '</span>';		
$text = preg_replace('#' .$startPoint . '(.*)' . $endPoint . '#i', 'Quantity Discount', $text);	
 echo $text;

I get two links printed to the screen.

 

Edit:  I'm an idiot and left off the last bracket of the closing span.  It works the same as strip_tags.  First a link, and then plain text.

Link to comment
Share on other sites

@@MrPhil

 

Same result. I looked at the ot_quantity_discount file that creates the span tags:

  function process() {
       global $order, $currencies;
       global $PHP_SELF;

       $od_amount = $this->calculate_deductions();
       if ($od_amount['total'] > 0) {
         reset($order->info['tax_groups']);
         while (list($key, $value) = each($order->info['tax_groups'])) {
           $tax_rate = $this->get_tax_rate_from_desc($key);
           if ($od_amount[$key]) {
             $order->info['tax_groups'][$key] -= $od_amount[$key];
             if ($this->calculate_tax != 'VAT') {
                $order->info['total'] -=  $od_amount[$key];
             }
           }
         }
         $order->info['total'] = $order->info['total'] - $od_amount['total'];
         $current_page = basename($PHP_SELF);
         if ($current_page != '' && $current_page == FILENAME_CHECKOUT_CONFIRMATION) {
         	
           //the following line conflicts with PDF customer invoice - disabling until resolution can be had.
            //$this->title = '<span class="orderEdit"><a href="javascript:alert(\'' . $this->explanation . '\');">' . $this->title . '</a></span>';
             } 
         $this->output[] = array('title' => $this->title . ':',
         'text' => '-' . $currencies->format($od_amount['total'], true, $order->info['currency'], $order->info['currency_value']),
         'value' => $od_amount['total']);
   
       }
    }

Unless that's converted in some weird way when it's accepted into the DB, those aren't <

 

I may just have to leave the discount explanation off permanently, which is bad, because there's -lots- of orders in the DB where the tags exist, and the explanation is useful.

Link to comment
Share on other sites

@@Supertex

 

Edit:  I'm an idiot and left off the last bracket of the closing span.  It works the same as strip_tags.  First a link, and then plain text.

Maybe I'm missing the point but isn't that correct? The preg_replace function is saying to replace everything in the span with the words Quantity Discount. If you want the final text to be a link without the span tag, then you need to use a link in the replacement text.

Support Links:

For Hire: Contact me for anything you need help with for your shop: upgrading, hosting, repairs, code written, etc.

Get the latest versions of my addons

Recommended SEO Addons

Link to comment
Share on other sites

Link to comment
Share on other sites

Oh, I see. Then it would seem something in that code is not behaving correctly. The following is a longer way to solve the problem. It doesn't use the preg function, which can be difficult to get to work at times. Maybe it will help.

$startPoint = '<span class="orderEdit">';
$endPoint = '</span>';		

$p1 = strpos($text, $startPoint);
$p2 = strpos($text, $endPoint, strlen($startpoint));
$text = substr_replace($text, 'Quantity Discount' , $p1, $p2 + strlen($endPoint));

Support Links:

For Hire: Contact me for anything you need help with for your shop: upgrading, hosting, repairs, code written, etc.

Get the latest versions of my addons

Recommended SEO Addons

Link to comment
Share on other sites

I have no idea at this point. If the < and > in the string are real (not replaced with < and > by htmlspecialchars() or htmlentities()), strip_tags() should find and remove them. If it's not, there's something really strange going on. All this assumes that you have carefully checked your work, and examined via the host file manager the actual file in question on the server, and it has been updated and you didn't load the edited copy to the wrong place or whatever...

Link to comment
Share on other sites

@@MrPhil @@Jack_mcs @@burt

 

Well I appreciate your suggestions, guys.  You've gone above and beyond, and I'm grateful.  This may just be one of those things that employs a permanent workaround.  At least I know it wasn't just me not knowing how...this just doesn't work.

 

I PM'd the developer...perhaps if he responds, he'll have some insight on why this file behaves the way it does.

 

Until then, I'll put this to rest.

Link to comment
Share on other sites

Well ...turns out this wasn't all that fruitless.  Were it not for the last attempt with strpos...I wouldn't have caught this.  It seems line 851 dealt with the numerals for the total only.  I was looking for the <span> to vanish...but I didn't notice that the subtotals became "Quantity Discount" at first glance.

 

Line 857 handles the text beside the figure, which was what I was trying to strip the tags from.  The code was in the right place, but I needed to be acting on $order->totals[$i][title] instead of [$i][text].

 

$order->totals[$i]['title'] = strip_tags($order->totals[$i]['title']);
 
goes above
 
$pdf->MultiCell(177,5,$order->totals[$i]['title'] . '  ' ,0,'R');
 
to do away with the code printout on the pdf invoice output file.

 

So if anyone else runs into this...you can thank the guys above for the solution.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...