skip to Main Content

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


  1. 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?

    Login or Signup to reply.
  2. 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:

    $DiscountDescriptionTrimmed  = "Free and Fast Shipping Club Member, A63540678, ";
    
    $pattern = '/^(e[a-zA-Z]{10})|(E[a-zA-Z]{8})|(A[a-zA-Z]{8})|([a-zA-Z-]{16})|([0-9]{8})/';
    
    preg_match($pattern, $DiscountDescriptionTrimmed, $match);
    
      for($i=1; $i<=5; $i++) {
        $len = strlen($match[$i]);
        if($len < 1) {
          continue;
        } else {
          // Scenario $i as you shown in the question
          /* for example */
          $_order->setDiscountDescription('Gift Cards ' . $DiscountDescription);
          break;
        }
    
      }
    

    (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 numbers

    If 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]

    Login or Signup to reply.
  3. Try below code. Hope it will help you.

    $text = "e7867445537, Free and Fast Shipping Club Member, A63540678, e7678 , Free and Fast Shipping Club Member, E67485536 , ET66U-UIK-66eh6YY,
    ET66UuUIKd66eh6YY, 99887765";
    
    function remove_zero($matches)
    {
        return 'Gift Cards ' .$matches[0];  
    }
    echo preg_replace_callback(
            "/(([d]{8}?)|([A][d]{8}?)|([e][d]{10}?)|([E][d]{8}?)|(([w]*[-]{1}[w]*[-]{1}[w]*)([S]{17})?))([DW]|$)/",
            "remove_zero",
            $text);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search