Manip de chaînes complexes
Fermé
ji_louis
-
22 juin 2010 à 16:25
Leviathan49 Messages postés 257 Date d'inscription jeudi 10 juin 2010 Statut Membre Dernière intervention 22 juillet 2011 - 25 juin 2010 à 10:33
Leviathan49 Messages postés 257 Date d'inscription jeudi 10 juin 2010 Statut Membre Dernière intervention 22 juillet 2011 - 25 juin 2010 à 10:33
A voir également:
- Manip de chaînes complexes
- Recherche automatique des chaînes ne fonctionne pas - Forum TNT
- Télécharger logiciel 4000 chaînes tv gratuit ✓ - Forum Cinéma / Télé
- Comment décrypter les chaines cryptées sur eutelsat 16a ✓ - Forum Satellite
- Pourquoi ma tv ne trouve pas toutes les chaînes - Forum Réception vidéo
- Impossible d'installer les chaines tv - Forum Vidéo/TV
2 réponses
Defouille
Messages postés
388
Date d'inscription
mercredi 13 janvier 2010
Statut
Membre
Dernière intervention
15 novembre 2011
54
25 juin 2010 à 10:00
25 juin 2010 à 10:00
Bonjour,
j'ai fais il y a quelque temps une fonction pour récupérer les attributs d'images dans du html, avec quelques modifications ou un traitement du résultat je pense que tu peux récupérer les informations dont tu as besoin :
TESTS :
En espérant que ça te soit utile.
j'ai fais il y a quelque temps une fonction pour récupérer les attributs d'images dans du html, avec quelques modifications ou un traitement du résultat je pense que tu peux récupérer les informations dont tu as besoin :
function imgs_data($html)
{
if($html == "")
return false ;
$imgs = array() ;
while(strpos($html, "<img") !== false)
{
$start_tag_img = strpos($html, "<img");
$end_tag_img = strpos($html, ">", $start_tag_img);
if($end_tag_img === false) // no end of tag img found
return false ;
$tag_img = substr($html, $start_tag_img, ($end_tag_img - $start_tag_img));
$res = array();
$res['src'] = content_att($tag_img, "src") ;
$res['title'] = content_att($tag_img, "title") ;
$res['width'] = content_att($tag_img, "width") ;
$res['height'] = content_att($tag_img, "height") ;
$res['class'] = content_att($tag_img, "class") ;
$imgs[] = $res ;
$html = substr($html, $end_tag_img) ;
}
return $imgs ;
}
function content_att($tag, $att)
{
if(strpos($att, "=") === false)
$att .= "=" ;
$start = strpos($tag, $att);
if($start === false) // no attribute in tag
return false ;
$start = $start +strlen($att)+1 ;
$delim = $tag[$start-1] ;
$end = strpos($tag, $delim, $start);
if($end === false) // no end of attribute in tag
return false ;
return substr($tag, $start, ($end - $start));
}
TESTS :
$html = "<p>du grand n'importe quoi <b>en HTML</b> commeici <br/> <img height=\"20\" src=\"http://uneURL/un_chemin/image1.jpg\" width=\"10\"> ou encore là <img src=\"../un_autre_chemin/image2.jpg\" height=\"20\" width=\"10\"></p>"; echo "<pre>"; print_r(imgs_data($html)); echo "</pre>";
En espérant que ça te soit utile.
Leviathan49
Messages postés
257
Date d'inscription
jeudi 10 juin 2010
Statut
Membre
Dernière intervention
22 juillet 2011
70
25 juin 2010 à 10:33
25 juin 2010 à 10:33
Ou sinon, tu peux utiliser une expression régulière :
$html = "<p>du grand n'importe quoi <b>en HTML</b> commeici <br/> <img height='20' src='http://uneURL/un_chemin/image1.jpg' width='10'> ou encore là <img src='../un_autre_chemin/image2.jpg' height='20' width='10'></p>";
preg_match_all("#<img\b.+src='.*/?([^/]+[.].{2,3})'#U", $html, $matches);
echo $matches[1];
echo $matches[2];
Dans le cas où on utilise des simples quotes.
Sinon pour l'exemple de Defouille :
preg_match_all("#<img\b.+src=\\\\".*/?([^/]+[.].{2,3})\\\\"#U", $html, $matches);
$html = "<p>du grand n'importe quoi <b>en HTML</b> commeici <br/> <img height='20' src='http://uneURL/un_chemin/image1.jpg' width='10'> ou encore là <img src='../un_autre_chemin/image2.jpg' height='20' width='10'></p>";
preg_match_all("#<img\b.+src='.*/?([^/]+[.].{2,3})'#U", $html, $matches);
echo $matches[1];
echo $matches[2];
Dans le cas où on utilise des simples quotes.
Sinon pour l'exemple de Defouille :
preg_match_all("#<img\b.+src=\\\\".*/?([^/]+[.].{2,3})\\\\"#U", $html, $matches);