Remplacer le host d'une URL
Bonjour,
Je cherche à remplacer le nom de domain d'une URL si celle-ci ne correspond pas à un pattern, exemple:
Code:
1 2
| $pattern = '#((?!maison)|(?<!maison))(?:\http(s)?://(www\.)?)?([-a-z0-9]+)\.[a-z]{2,6}$#i';
$str = preg_replace($pattern, 'google', $str); |
Remplacer par "google.com" si ne correspondants pas a "maison.com" sans modifier la syntaxe de URL.
Je n'arrive pas à couvrir l'ensemble des syntaxes (sous-domaine, répertoire, IP, etc.), protocole (http, https, ftp, etc.), DNS (.fr, .com, .info, org.au, etc.) ...
J'ai fait un petit bout de code pour tester le pattern:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| function mask($str) {
$pattern = '#((?!maison)|(?<!maison))(?:\http(s)?://(www\.)?)?([-a-z0-9]+)\.[a-z]{2,6}$#i';
$str = preg_replace($pattern, 'google', $str);
return $str;
}
$string = "domain.fr http://subdomain.maison.com domain.com domain.info domain.org.au www.domain.fr www.domain.com www.domain.info. www.domain.org.au http://domain.fr http://domain.com http://domain.info http://domain.org.au http://www.domain.fr http://www.domain.com http://www.domain.info http://www.domain.org.au subdomain.domain.fr subdomain.domain.com subdomain.domain.info subdomain.domain.org.au www.subdomain.domain.fr www.subdomain.domain.com www.subdomain.domain.info www.subdomain.domain.org.au http://subdomain.domain.fr http://subdomain.domain.com http://subdomain.domain.info http://subdomain.domain.org.au/folder/ http://subdomain.domain.org.au/folder/subfolder/ http://subdomain.domain.org.au http://www.subdomain.domain.fr http://www.subdomain.domain.fr/folder/ http://www.subdomain.domain.com http://www.subdomain.domain.info http://www.subdomain.domain.org.au ";
$arr = explode(' ', $string);
echo '<table><caption>Remplacer par « google.com » les noms de domaines ne correspondants pas a « maison.com » sans modifier la syntaxe des URLs</caption><thead><tr><th>Old</th><th>New</th></thead>';
foreach($arr as $value) {
echo '<td>'.$value.'</td><td>'.mask($value)."</td></tr>";
}
echo '</table>'; |
Merci d'avance pour votre aide.
Cordialement