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
Bonjour,

Je récupère une longue chaîne de caractère (du html) dans un textarea, et à l'intérieur, je voudrais:
- trouver le nom des images
- remplacer leur chemin par le nom de mon répertoire rep
exemple:

<p>du grand n'importe quoi <b>en HTML</b> comme
ici <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>

où il faudrait:
- trouver image1.jpg et image2.jpg
- remplacer http://uneURL/un_chemin et ../un_autre_chemin par rep

Je ne maîtrise pas assez les expressions régulières pour y arriver. Est-ce que quelqu'un pourrait m'aider, s'il vous plait?

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
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 :

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.
0
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
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);
0