Précédent   Forum des professionnels en informatique > PHP > Langage > Regex
Regex Forum d'entraide sur les expressions rationnelles PHP. Avant de poster -> FAQ regex, Cours de regex et Sources de regex
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 19/01/2012, 15h36   #1
Membre régulier
 
Avatar de la_chouette
 
Inscription : avril 2010
Messages : 114
Détails du profil
Informations forums :
Inscription : avril 2010
Messages : 114
Points : 76
Points : 76
Par défaut 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 &laquo; google.com &raquo; les noms de domaines ne correspondants pas a &laquo; maison.com &raquo; 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
__________________
La vitesse de la lumière étant supérieure à celle du son, beaucoup de gens sont brillants jusqu'à ce qu'ils ouvrent leur bouche.
la_chouette est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 31/01/2012, 12h05   #2
Membre régulier
 
Avatar de la_chouette
 
Inscription : avril 2010
Messages : 114
Détails du profil
Informations forums :
Inscription : avril 2010
Messages : 114
Points : 76
Points : 76
Par défaut Résolu

Code :
'#(?![maison])([-a-z0-9]{2,63}\.(com|net|org|fr|info|be|biz|pro|asso))#i'
Résolu
__________________
La vitesse de la lumière étant supérieure à celle du son, beaucoup de gens sont brillants jusqu'à ce qu'ils ouvrent leur bouche.
la_chouette est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 02h38.


 
 
 
 
Partenaires

Hébergement Web