How can you extract only the numbers from a string in PHP?

To extract only the numbers from a string in PHP, you can use the preg_replace function along with a regular expression pattern to remove all non-numeric characters from the string. This will leave you with only the numbers in the string.

$string = "abc123def456ghi";
$numbers = preg_replace('/[^0-9]/', '', $string);
echo $numbers;