Replace corrupted alphanumeric id substring containing unwanted spaces

96 Views Asked by At

I have content that is coming dynamic but some characters from string are missing. I want to replace that characters to make it correct. I have complete order id but in content it's coming with space.

  1. I need to replace order id that is coming with space with correct order id.
  2. I have correct order id

This is example content:

$content = "Order is purchased on 23-05-2023.Order is in progress&nbsp and you can see order status on website by enter id JKSZ089P6X07   012";

$content = "Order Model is Ford and year model 2017.Order id is 6FTYF75C7HD  715 and Vehicle type is commercial.";                     

$content = "Order Model is Mahindra and year model 2015.Order id is 9PKKJ7FS3CD  890 and Vehicle type is personal.";

My code:

$orderId = "JKSZ089P6X07A07012";
$getChar =  substr(orderId ,0,12);

if(strpos( $content, $getChar ) !== false ) { 
    echo "found";
    $content = str_replace($getChar," ",$content);
}

echo $content;

Output content should be

$content = "Order is purchased on 23-05-2023.Order is in progress and you can see order status on website by enter id JKSZ089P6X07A07012";

$content = "Order Model is Ford and year model 2017.Order id is 6FTYF75C7HD680715 and Vehicle type is commercial.";                   

$content = "Order Model is Mahindra and year model 2015.Order id is 9PKKJ7FS3CD347890 and Vehicle type is personal.";
2

There are 2 best solutions below

5
mickmackusa On BEST ANSWER

Your set of sample input data indicates that your whitespace problem consistently occurs after the 12 character.

To repair the string using the $orderId, create a regex pattern that replaces the 3 characters after the 12th character with a "one-or-more whitespace expression". I am wrapping the pattern in word boundaries to eliminate the chance of making false-positive replacements (mangling unintended substrings). If there is a match, replace it with your known $orderId value.

Code: (Demo)

$regex = '#\b' . substr_replace($orderId, '\s+', 12, 3) . '\b#';

var_export(preg_replace($regex, $orderId, $content));

If the spaces to be replaced are actually  , then here is the modification of my above regex pattern. (Demo)

$regex = '#\b' . substr_replace($orderId, '(?: )+', 12, 3) . '\b#';
2
lukas.j On
function correctString(string $content): false|string {

  $len = 18;       // The length of the id
  $minLen1 = 12;   // The minimum length of the first part of the malformed id
  $minLen2 = 3;    // The minimum length of the second part of the malformed id
                   // Of course minLen1 + minLen2 should be smaller than $len

  preg_match_all("/[A-Z0-9]{{$len}}/", $content, $matches);

  if (count($matches[0]) === 2 && $matches[0][0] === $matches[0][1]) {

    // Found valid id, and also the second, and it they are identical
    return $content;

  } elseif (count($matches[0]) === 1) {

    // Found valid id, but not the second one
    $part1 = substr($matches[0][0], 0, $minLen1);
    $part2 = substr($matches[0][0], $len - $minLen2, $minLen2);
    preg_match_all("/$part1 $part2/", $content, $matches2);

    if (count($matches2[0]) === 1) {

      // Found second, but malformed id
      return str_replace($matches2[0][0], $matches[0][0], $content);
      
    } else {

      // Found no second malformed id
      return false;

    }

  } else {

    // Found no id at all
    return false;

  }

}

echo correctString('Order ... is JKSZ089P6X07A07012.Order is ... id JKSZ089P6X07 012') .  "\n";
echo correctString('Order ... is JKSZ089P6X07A07012.Order is ... id JKSZ089P6X07A07012') .  "\n";
echo correctString('Order ... is JKSZ089P6X07A0701.Order is ... id JKSZ089P6X07 012') .  "\n";

Output:

  Order ... is JKSZ089P6X07A07012.Order is ... id JKSZ089P6X07A07012
  Order ... is JKSZ089P6X07A07012.Order is ... id JKSZ089P6X07A07012
                      <- Echoing false prints as an empty line