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
sharingsunshineAsked:
Who is Participating?
 
Julian HansenCommented:
What we are trying to do here is reduce the number of comparisons needed to be made to determine if the input is correct.

It is unnecessary to have a separate process for comparing PO BOX and po box as they are essentially identical except for case so using a case insensitive compare will greatly reduce the number of comparisons needed to be made.

What you need to ask is are you going to allow for all possible variations or try and target the majority and coax clients to use one of the acceptable set.

For instance valid input on a PO box would be

PO Box ###
OR
P.O. Box ###

Ignoring case.
A regex for this might therefore be
/^p\.?\s*o\.?\s+box\s+\d+$/i

Open in new window

This regex is saying
match all strings that begin with a 'p' (any case by the /i at the end of the expression)
followed optionally by a '.' and 0 or more spaces (ignore white space)
followed by an 'o' (again no case)
followed optionally by a '.' and at least 1 space
followed by the word 'box' (no case)
followed by at least 1 space
followed by 1 or more digits.

The ^ at the beginning of the expression says match from the start of the string
The $ at the end says match to the end of the line
The /i means ignore case

This example demonstrates how to use it
<?php
$input = array(
"PO Box 123",
"P.O. Box 123",
"PO. Box 123",
"P.O Box 123",
"P O Box 123",
"P Box 123",
"PO Box123",
"PO Box",
"P. Box 123",
"po box 123",
"p.o. box 123",
"po. box 123",
"p.o box 123",
"p o box 123",
"p box 123",
"po box123",
"po box",
"p. box 123"
);

$pattern = "/^p\.?\s*o\.?\s+box\s+\d+$/i";

foreach($input as $i) {
	if (preg_match($pattern, $i, $matches)) {
		echo "{$i} is a match" . print_r($matches, true) . "<br>";
	}
	else {
		echo "{$i} failed to match<br>";
	}
}

Open in new window

Output
PO Box 123 is a matchArray ( [0] => PO Box 123 )
P.O. Box 123 is a matchArray ( [0] => P.O. Box 123 )
PO. Box 123 is a matchArray ( [0] => PO. Box 123 )
P.O Box 123 is a matchArray ( [0] => P.O Box 123 )
P O Box 123 is a matchArray ( [0] => P O Box 123 )
P Box 123 failed to match
PO Box123 failed to match
PO Box failed to match
P. Box 123 failed to match
po box 123 is a matchArray ( [0] => po box 123 )
p.o. box 123 is a matchArray ( [0] => p.o. box 123 )
po. box 123 is a matchArray ( [0] => po. box 123 )
p.o box 123 is a matchArray ( [0] => p.o box 123 )
p o box 123 is a matchArray ( [0] => p o box 123 )
p box 123 failed to match
po box123 failed to match
po box failed to match
p. box 123 failed to match

Open in new window

0
 
Dillyn BarberCIOCommented:
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];
0
 
Julian HansenCommented:
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?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
sharingsunshineAuthor Commented:
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.
0
 
Julian HansenCommented:
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()
0
 
sharingsunshineAuthor Commented:
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.
0
 
sharingsunshineAuthor Commented:
Thanks for the explanation.  That will save a lot of steps.
0
 
Julian HansenCommented:
You are welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.