jcadima
4/26/2018 - 4:19 PM

Search and Replace Youtube duration time string


https://stackoverflow.com/questions/7605480/str-replace-for-multiple-items

<?php

$string = 'PT4S';  // Seconds
$string2 = 'PT1M16S' ; // Minutes , Seconds
$string3 = 'PT2H28M12S' ; // Hours, Minutes, Seconds

// 1) remove "PT"
// 2) Replace H=>Hour(s), M=>Minute(s), S=>Second(s)

$formatted = str_replace(
	array("PT", "H", "M","S"), // search for
	array("", "Hour(s), " ,"Minutes(s), ", "Seconds"),  // replace
	$string3 // the string
);

echo  $formatted; // 2Hour(s), 28Minutes(s), 12Seconds
// 1Minutes(s), 16Seconds
// 4Seconds