I'm picking up through a PHP parser with DOM all the links on a page. I need to pick only the links that contain the word "buy-toyota"
I was using the following code, but suddenly it stopped working:
//array que pega o valor do parser
$toyota =array();
$dom1 = new DOMDocument();
$dom1->loadHTMLFile('http://www.webmotors.com.br/comprar/carros/novos-usados/sp-sao-paulo/toyota/?tipoveiculo=carros&anunciante=pessoa%20f%C3%ADsica&tipoanuncio=novos%7Cusados&estado1=s%C3%A3o%20paulo&marca1=toyota&anode=2012&anoate=&kmde=&kmate=30000&p=1&o=1&qt=36');
// Consultando os links
$links1 = $dom1->getElementsByTagName('a');
foreach ($links1 as $link) {
//aqui pega o link
$string = $link->getAttribute('href').PHP_EOL;
//apenas verifica se o link nao tem a string "comprar/toyota"
if(strpos($string, 'comprar/toyota') != 1){
//nadafaz;
}else{
//verifica se o link tem a string "comprar/toyota"
if(strpos($string, 'comprar/toyota') == 1){
//guarda a string encontrada
$linky = $string;
$n++;
#echo $n."<br>";
//insere o link no array
$toyota[$n] = $linky;
//imprime oo link
echo $toyota[$n]."<br>";
}
}
}
Then I inverted the condition of the strpos in my if like this:
//array que pega o valor do parser
$toyota =array();
$dom1 = new DOMDocument();
$dom1->loadHTMLFile('http://www.webmotors.com.br/comprar/carros/novos-usados/sp-sao-paulo/toyota/?tipoveiculo=carros&anunciante=pessoa%20f%C3%ADsica&tipoanuncio=novos%7Cusados&estado1=s%C3%A3o%20paulo&marca1=toyota&anode=2012&anoate=&kmde=&kmate=30000&p=1&o=1&qt=36');
// Consultando os links
$links1 = $dom1->getElementsByTagName('a');
foreach ($links1 as $link) {
//aqui pega o link
$string = $link->getAttribute('href').PHP_EOL;
//apenas verifica se o link nao tem a string "comprar/toyota"
if(strpos($string, 'comprar/toyota') == 1){
//nadafaz;
}else{
//verifica se o link tem a string "comprar/toyota"
if(strpos($string, 'comprar/toyota') != 1){
//guarda a string encontrada
$linky = $string;
$n++;
#echo $n."<br>";
//insere o link no array
$toyota[$n] = $linky;
//imprime oo link
echo $toyota[$n]."<br>";
}
}
}
Now you are pulling all the links on the page ... I needed to pick only the ones with the "buy-toyota" string anywhere on the link
How can I do this?