PHP: how to fix error: A non-numeric value encountered


Example: to parse youtube duration format to seconds, we use this method:

echo preg_replace_callback("/^PT(?:(\d+)H)?(?:(\d+)M)?(\d+)S$/",function($matches){
    return 3600*$matches[1]+60*$matches[2]+$matches[3];
},$duration);

This method work well with PHP5, but i use PHP7 i get error:

$duration:PT9M37S
In autoreup.php line 421:

  A non-numeric value encountered

A non-numeric value encountered

How to fix this?

Simple, you must convert theses variables to int:

echo preg_replace_callback("/^PT(?:(\d+)H)?(?:(\d+)M)?(\d+)S$/",function($matches){
    return 3600*(int)$matches[1]+60*(int)$matches[2]+(int)$matches[3];
},$duration);

Leave a Reply