How does using regular expressions (RegEx) improve the process of converting a UPC to an ISBN13 in PHP?

Regular expressions can be used to extract the digits from a UPC and then convert those digits into an ISBN13 format by adding the necessary prefix and checksum. This can improve the process of converting a UPC to an ISBN13 in PHP by providing a more efficient and accurate way to manipulate the UPC data.

$upc = "123456789012";
$upcDigits = preg_replace("/[^0-9]/", "", $upc); // Extract only digits from UPC

// Add prefix '978' and calculate checksum
$isbn13 = "978" . substr($upcDigits, 0, -1);
$checksum = 0;
for ($i = 0; $i < 12; $i++) {
    $checksum += $isbn13[$i] * (($i % 2 == 0) ? 1 : 3);
}
$checksum = (10 - ($checksum % 10)) % 10;
$isbn13 .= $checksum;

echo $isbn13;