How can PHP developers ensure that only numbers are extracted from an alphanumerical string while preserving the alphabetical characters in a separate string?

To extract only numbers from an alphanumeric string in PHP, developers can use regular expressions to match and extract the numerical characters. By using the preg_replace function, developers can remove all non-numeric characters from the string, leaving only the numbers. Additionally, developers can use the preg_replace function with a regular expression pattern to remove all numeric characters from the string, leaving only the alphabetical characters in a separate string.

<?php
// Input alphanumeric string
$inputString = "abc123def456ghi";

// Extract only numbers
$numbersOnly = preg_replace('/[^0-9]/', '', $inputString);

// Extract only alphabetical characters
$lettersOnly = preg_replace('/[0-9]/', '', $inputString);

// Output the results
echo "Numbers Only: " . $numbersOnly . "\n";
echo "Letters Only: " . $lettersOnly;
?>