8791 sujets

Développement web côté serveur, CMS

hello, j'aimerais extraire d'une chaine toute la balise img.
J'ai ce code :

$post = '<div class="wpg2tag-image"><a href="monlien"><img src="http://monsite.com/main.php?g2_view=core.DownloadItem&amp;g2_itemId=357&amp;g2_serialNumber=2&amp;g2_GALLERYSID=786ab243988914199985c29dc58dbbca" width="150" height="85" id="IFid1" class="ImageFrame_none" alt="Castleinthesky" longdesc="Le château dans le ciel"/></a></div><p>montexte ici</p>';

if (preg_match("/\< *[img][^\>]*[src] *= *[\"\']{0,1}([^\"\'\ >]*)/i" ,$post,$matches)) {			

	echo $matches[0];


Mais étrangement, cela me retourne

<img src="http://monsite.com/main.php?g2_view=core.DownloadItem&amp;g2_itemId=357&amp;g2_serialNumber=2&amp;g2_GALLERYSID=786ab243988914199985c29dc58dbbca" width="150" height="85" id="IFid1" class="ImageFrame_none" alt="Castleinthesky" longdesc="Le


Pourquoi le texte est il tronqué à longdesc="Le ?

On dirait qu'il plante parce qu'il y a un espace... J'ai vérifié en raccourcissant mon URL, ce n'est pas un problème de longueur de chaîne...
Modifié par gordie (11 Nov 2007 - 15:24)
J'ai essayé ce code aussi (d'après le générateur regexp), qui ne marche pas non plus :

<?php

  $txt='<a href="http://monsite.com/?page_id=208&amp;g2_itemId=356" title="Castleinthesky"><img src="http://folioscope.awn.com/gallery/main.php?g2_view=core.DownloadItem&amp;g2_itemId=357&amp;g2_serialNumber=2&amp;g2_GALLERYSID=786ab243988914199985c29dc58dbbca" width="150" height="85" id="IFid1" class="ImageFrame_none" alt="Castleinthesky" longdesc="Le château dans le ciel"/>';

  $re1='.*?';	# Non-greedy match on filler
  $re2='(longdesc)';	# Word 1
  $re3='.*?';	# Non-greedy match on filler
  $re4='(".*?")';	# Double Quote String 1

  if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4."/is", $txt, $matches))
  {
      $word1=$matches[1][0];
      $string1=$matches[2][0];
      print "($word1) ($string1) \n";
  }

  #-----
  # Paste the code into a new php file. Then in Unix:
  # $ php x.php 
  #-----
?>

Modifié par gordie (11 Nov 2007 - 15:55)
Ok, ça marche avec ceci.
Le seul problème restant est que ça me sort les chaines avec des "" autour; et je n'en veux pas...

<?php
$post = '<div class="wpg2tag-image"><img src="http://monsite.com/main.php?g2_view=core.DownloadItem&amp;g2_itemId=357&amp;g2_serialNumber=2&amp;g2_GALLERYSID=786ab243988914199985c29dc58dbbca" width="150" height="85" id="IFid1" class="ImageFrame_none" alt="Castleinthesky" longdesc="Le château dans le ciel"/></div><p>Retenue prisonnière.</p>';

if (preg_match("|(<img[^>]*?/>)|i", $post, $result)) {

	list(, $img_tag) = $result;
	
	//echo $img_tag;
	
	$rgxp='.*?(src).*?(".*?")';	# Double Quote String 1
	if (preg_match_all ("/".$rgxp."/is", $img_tag, $matches)) {
		$image_tag_src=$matches[2][0];
		echo "SRC:".$image_tag_src."<br>";
	}

	$rgxp='.*?(alt).*?(".*?")';	# Double Quote String 1
	if (preg_match_all ("/".$rgxp."/is", $img_tag, $matches)) {
		$image_tag_alt=$matches[2][0];
		echo "ALT:".$image_tag_alt."<br>";
	}
	
	$rgxp='.*?(longdesc).*?(".*?")';	# Double Quote String 1
	if (preg_match_all ("/".$rgxp."/is", $img_tag, $matches)) {
		$image_tag_longdesc=$matches[2][0];
		echo "LONGDESC:".$image_tag_longdesc."<br>";
	}
	
}
?>