...

View Full Version : Why is this outputting the wrong stuff?



DoA
03-20-2010, 12:36 AM
$Target = "phrase 2"
$textarea_array=array("phrase 1","phrase 2");

foreach ($textarea_array as $XQuery) {


$html = "http://www.mysite.co.uk/search?api=" .$XQuery;
$dom = new DOMDocument;
@$dom->loadHTMLFile($html);
$xpath = new DOMXPath($dom);
$aTag = $xpath->query('//h3[@class="r"]/a');
foreach ($aTag as $val) {
$arr[] = $val->getAttribute('href');
}
foreach($arr as $key => $value)
{
if(stristr($value, $Target))
{
$Position = $key;
break;
}
else {
$Position = "Not Found";
}
}
echo $XQuery;
echo "-";
echo $Position;
echo "<br><br>";
}


What should be being output is

phrase 1 - position of phrase 1
phrase 2 - position of phrase 2

What is actually being output is

phrase 1 - position of phrase 1
phrase 2 - position of phrase 1

And I just can see why!?

met
03-20-2010, 01:41 PM
my guess is because of



if(stristr($value, $Target))
{
$Position = $key;
break; // this


quote php ~

break ends execution of the current for, foreach, while, do-while or switch structure.

so it's only "for-eaching" once, hence why position is the same for both.

edit ~ but this is just speculation



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum