How can you convert a string to an integer in PHP only if the string contains numbers?

To convert a string to an integer in PHP only if the string contains numbers, you can use the `is_numeric()` function to check if the string contains numbers before converting it to an integer using the `intval()` function. This way, you can ensure that you are converting a valid numeric string to an integer.

$string = "123abc";

if (is_numeric($string)) {
    $integer = intval($string);
    echo $integer;
} else {
    echo "String does not contain numbers.";
}