Link to home
Start Free TrialLog in
Avatar of sharingsunshine
sharingsunshineFlag for United States of America

asked on

PHP Parse error

I am getting this error with the code below
PHP Parse error:  syntax error, unexpected 'box\xe2\x80\x99' (T_STRING) in /path to/wordpress/wp-content/themes/flatsome-child/functions.php on line 10

Open in new window



<?php
// Add custom Theme Functions here
add_filter('woocommerce_package_rates', 'wf_hide_fedex_for_po_box_shipment', 10, 2);
function wf_hide_fedex_for_po_box_shipment($available_shipping_methods, $package){
    $shipping_method_to_hide = 'wf_fedex_woocommerce_shipping';
    global $woocommerce;
    $address  = ( !empty( $woocommerce->customer->get_shipping_address_1() ) ) ? $woocommerce->customer->get_shipping_address_1() : $woocommerce->customer->get_billing_address_1();
    $postcode = ( !empty( $woocommerce->customer->get_shipping_postcode() ) ) ? $woocommerce->customer->get_shipping_postcode() : $woocommerce->customer->get_billing_postcode();

   if( strstr( $address, ‘pobox’ ) || strstr( $postcode, ‘pobox’ ) || strstr( $address, ‘po box’ ) || strstr( $postcode, ‘po box’ ) || strstr( $address, ‘p.o. box’ ) || strstr( $postcode, $
strstr( $address, ‘P.o. box’ ) || strstr( $postcode, ‘P.o. box’ ) || strstr( $address, ‘po. box’ ) || strstr( $postcode, ‘po. box’ ) || strstr( $address, ‘Po Box.’ ) || strstr( $postcode, $
{
        foreach ($available_shipping_methods as $shipping_method => $value) {
            if( strpos( $shipping_method, $shipping_method_to_hide ) !== false ) {
                unset($available_shipping_methods[$shipping_method]);
            }
        }
    }
    return $available_shipping_methods;
}

Open in new window


Line 10 is where the if statement begins.  I have a regex I used in a different cart.  What would be the syntax to add this method?  I am new to oop.

static public function is_pobox_address($address) {
                $po_pattern1 = '/^(([pP]{1}(.*?)(\s+)?[oO]{1}(.*?))+?(\s+)?([0-9]+))+?/';
                $po_pattern2 = '/^([bB][oO]?[xX]?)(\s+)?([0-9]+)+?/';
                if (preg_match($po_pattern1, $address) || preg_match($po_pattern2, $address)) {
                        return true;
                }
                else {
                        return false;

Open in new window


Help me to stop this error.  Thanks
Avatar of Dillyn Barber
Dillyn Barber
Flag of United States of America image

Worth a shot doing your regex along the lines of this:
preg_match('/^(([pP]{1}(.*?)(\s+)?[oO]{1}(.*?))+?(\s+)?([0-9]+))+?/', $po_pattern1, $address);
$the_preg = $match[1];
preg_match('/^([bB][oO]?[xX]?)(\s+)?([0-9]+)+?/', $po_pattern2, $address);
$the_preg2 = $match[2];  

Open in new window


Results may vary, doing it this way works for me every time though. Then you can just return or echo $match[1]; or $match[2];
Avatar of Julian Hansen
Rather use stristr() instead of strstr() - it reduces your comparisons to only the lower case versions of the string.

Your question seems to have two parts
1. You have an error
2. You want to incorporate the RegEx for the PO Address

Can we focus on just one of these for now.

Where is the string box\xe2\x80\x99 in your source - the error is referring to it - but it is not there?
Avatar of sharingsunshine

ASKER

Dillyn, can you show how this would be implemented into my code?

Julian, you are correct the string of code making the error was obvious.  It turns out my copy and paste from the original was incomplete.  Since my comparisons are upper and lower case I won't be able to use your suggestion.
Since my comparisons are upper and lower case I won't be able to use your suggestion.
I don't understand this bit - why is it important whether you compare PO BOX to po box and not get a match?

My suggestion is to reduce the comparison to a case neutral one to reduce the number of possible combinations. To do that you use a function that is case agnostic - such as stristr()
The customer can decide to put in the phrase PO Box in many different iterations.  It is my "limited" understanding of php that the if test logic would be case sensitive.  Thus, the reason for my statement.

If this is incorrect then please by all means please explain.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the explanation.  That will save a lot of steps.
You are welcome.