Попробуйте этот код:
$sites = array(
array('domain' => 'www.yahoo.com', 'title' => 'Yahoo!'),
array('domain' => 'www.thegreenpages.com', 'title' => 'Welcome to The Green Pages.'),
array('domain' => 'www.experts-exchange.com', 'title' => 'Experts Exchange - The #1 resource on the web for solving technology problems.'),
);
foreach ($sites as $idx => $site) {
$domain = preg_replace('/^www\./i', '', $site['domain']);
$domain = preg_replace('/\.(com|net|org|info|us)$/i', '', $domain);
$expression = '/';
for ($i = 0; $i < strlen($domain); $i++) {
$char = $domain[$i];
$expression .= $char . (ctype_alpha($char) ? '' : '?');
$expression .= '\s*';
}
$expression .= '/i';
preg_match($expression, $site['title'], $matches);
$sites[$idx]['name'] = $matches[0];
}
Если вы print_r($sites)
, вы получите:
Array
(
[0] => Array
(
[domain] => www.yahoo.com
[title] => Yahoo!
[name] => Yahoo
)
[1] => Array
(
[domain] => www.thegreenpages.com
[title] => Welcome to The Green Pages.
[name] => The Green Pages
)
[2] => Array
(
[domain] => www.experts-exchange.com
[title] => Experts Exchange - The #1 resource on the web for solving technology problems.
[name] => Experts Exchange
)
)
Независимо от того, что вам нужно будет настроить свой сценарий, пока вы не получите его правильно, но это место для начала.