Enhance and Shrink Preg_match lines I made a preg_match to get my results and it works fine but I think it can be done with simple lines instead of repeating the code

$input = array('http://example.com/blue-bloods-s10e13-more-text/','http://example.com/charmed-2018-s02e11-more-text/','http://example.com/dynasty-2017-s03e11-more-text/','http://example.com/magnum-p-i-2018-s02e14-more-text/','http://example.com/fresh-off-the-boat-s06e13-more-text/','http://example.com/hawaii-five-0-2010-s10e14-more-text/','http://example.com/american-housewife-s04e13-more-text/','http://example.com/love-us-s01e13-more-text/');

    foreach ($input as $value ){
preg_match('/http:\/\/example.com\/([^`]*?)-s([0-9]{2})e([0-9]{2})/', $value, $matches);

$name = $matches[1];
$cname = preg_replace('/-us/', '', $name);
$cname1 = preg_replace('/-/', ' ', $cname);
$cname2 = preg_replace('/([0-9]{4})$/', '', $cname1);
$cname3 = preg_replace('/\s$/', '', $cname2);

echo $cname3."&lt;br&gt;";
}


The output is exactly like what I want

blue bloods
charmed
dynasty
magnum p i
fresh off the boat
Hawaii five 0
american housewife
love
Toward optimization and better functionality


consider using short array syntax [] instead of old-fashioned array() 
do not designate redundant captured groups such as ([0-9]{2})...([0-9]{2}) if you only need the 1st captured group ([^`]*?)
regex [0-9] has a concise equivalent for designating digits - \d


All those preg_replace calls in the initial approach can be combined into a single call using regex alternation group /-us|(\d{4}|\s)$/ for combining the needed patterns. The \s$ patten could also be excluded from combined pattern in favor of rtrim function call.As /-/ is the only pattern that is too simple and requires different replacement string ' ' (space) - it deserves to be a simple string replacement with str_replace function.



The final optimized approach:

$input = ['http://example.com/blue-bloods-s10e13-more-text/','http://example.com/charmed-2018-s02e11-more-text/',
          'http://example.com/dynasty-2017-s03e11-more-text/','http://example.com/magnum-p-i-2018-s02e14-more-text/', 
          'http://example.com/fresh-off-the-boat-s06e13-more-text/','http://example.com/hawaii-five-0-2010-s10e14-more-text/',
          'http://example.com/american-housewife-s04e13-more-text/','http://example.com/love-us-s01e13-more-text/'];

foreach ($input as $value){
    preg_match('/http:\/\/example.com\/([^`]*?)-s\d{2}e\d{2}/', $value, $matches);
    $name = str_replace('-', ' ', preg_replace('/-us|(\d{4}|\s)$/', '', $matches[1]));
    echo $name . "&lt;br&gt;";
}


The output:

blue bloods
charmed
dynasty
magnum p i
fresh off the boat
hawaii five 0
american housewife
love