Im faire un script pour obtenir d'autres pages de contenu, et im en ce moment à travailler sur une fonction qui devrait obtenir le contenu de l'étiquette ... mais im un peu coincé: DComment obtenir un contenu de tag?
found a new tag of same kind inside tag...
nothing found...
1111
2222
est imprimé.
<?php
function d($toprint)
{
echo $toprint."<br />";
}
function GetTagContents($source, $tag, $pos)
{
$startTagPos = strpos($source, "<".$tag, $pos);
$startTagEndPos = strpos($source, ">", $startTagPos)+1;
$endTagPos = strpos($source, "</".$tag, $startTagEndPos);
$lastpos = $startTagPos+1;
while($lastpos != False)
{
$newStartTagPos = strpos($source, "<".$tag, $lastpos);
if($newStartTagPos == False)
{
d("nothing found...");
$lastpos = False;
}
else if($newStartTagPos > $endTagPos)
{
d("out of bounds...");
$lastpos = False;
}
else
{
d("found a new tag of same kind inside tag...");
$lastpos = $newStartTagPos+1;
$endTagPos = strpos($source, "</".$tag, $newStartTagPos);
}
}
return substr($source, $startTagEndPos, $endTagPos-$startTagEndPos);
}
?>
<html>
<body>
<?php
d(GetTagContents('<div>1111<div>2222</div>3333</div>', "div", 0));
?>
</body>
</html>
quelqu'un a-t-il des idées?
http://stackoverflow.com/questions/1732348/regex-match -open-tags-sauf-xhtml-self-contained-tags/1732454 # 1732454/moi à la rescousse – zerkms
Btw, si vous voulez vraiment analyser manuellement - vous pouvez commencer à lire http://en.wikipedia.org/wiki/Finite-state_machine – zerkms
vous pouvez utiliser la classe simplexml pour votre raison – AmirModiri