I have a variable called $DiscountDescriptionTrimmed
that could, possibly, contain data in formats such as:
“Free and Fast Shipping Club Member, A63540678, “
Or
“A63540678, “
I’d like to find the Gift Card number in this variable (Example: A63540678) and perform logic based on this, append ‘Gift Cards’ to the beginning of $DiscountDescription
.
Currently, my code only takes into account if the Gift Card number is in the front of the variable. Not if it’s in any other position (after the comma for instance).
There must be a RegEx way of doing this PHP code better, right? I have various Gift Card scenarios, listed below in my code, that mainly consist of the Gift Card starting with a particular letter or number, and being of a certain length.
My current PHP code is:
$Description = $item->getName();
$DiscountDescription = $_order->getDiscountDescription();
$DiscountDescriptionTrimmed = strtok($DiscountDescription,', ');
if ($DiscountDescriptionTrimmed != '') {
if (substr($DiscountDescriptionTrimmed,0,1) === "e" && strlen($DiscountDescriptionTrimmed) === 11){
$_order->setDiscountDescription('Gift Cards ' . $DiscountDescription);
}
elseif (substr($DiscountDescriptionTrimmed,0,1) === "E" && strlen($DiscountDescriptionTrimmed) === 9){
$_order->setDiscountDescription('Gift Cards ' . $DiscountDescription);
}
elseif (substr($DiscountDescriptionTrimmed,0,1) === "A" && strlen($DiscountDescriptionTrimmed) === 9){
$_order->setDiscountDescription('Gift Cards ' . $DiscountDescription);
}
elseif (strlen($DiscountDescriptionTrimmed) === 17 && substr_count($DiscountDescriptionTrimmed,'-') === 2){
$_order->setDiscountDescription('Gift Cards ' . $DiscountDescription);
}
elseif (strlen($DiscountDescriptionTrimmed) === 8 && ctype_digit($DiscountDescriptionTrimmed)){
$_order->setDiscountDescription('Gift Cards ' . $DiscountDescription);
}
}
Gift Card Scenarios:
Scenario 1: if the gift card starts with “e” and is 11 characters in length.
Scenario 2: if the gift card starts with “E” and is 9 characters in length.
Scenario 3: if the gift card starts with “A” and is 9 characters in length.
Scenario 4: if the gift card is 17 characters long and has two “-” dashes in it.
Scenario 5: if the gift card is 8 characters long and consists of only numbers.
3
Answers
I don’t know PHP, but a quick search of its documentation reveals that it uses perl-style regular expression syntax and also has functions to perform search and replace, e.g. function preg_replace. The documentation I found is located
here
Have you looked at the documentation?
If you have, did it not help you?
The task is
(1) filter out the card number from a string;
(2) Use the card number for different scenarios. Here is what I got for you:
(e[a-zA-Z]{10})
, e + 10 letters(E[a-zA-Z]{8})
, E + 8 letters(A[a-zA-Z]{8})
, A + 8 letters([a-zA-Z-]{16})
, 16 letters + ‘-‘([0-9]{8})
, 8 numbersIf the card number is composed of only digits, not letters, (character is ambiguous), then replace
[a-zA-Z]
with[0-9]
If it is a mixture, then use
[0-9a-zA-Z]
Try below code. Hope it will help you.