How to get only strings that contain the word "x"

0

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?

    
asked by anonymous 19.10.2015 / 17:25

3 answers

2

Replace

//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>";
            }                   
}

POR

// apenas verifica se o link nao tem a string "comprar/toyota" 
if (strpos($string, 'comprar/toyota') !== false) {
    //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>";
}
    
19.10.2015 / 17:44
1

As it says in the manual on strpos :

  

Returns the numeric position of the first occurrence of needle within haystack.   If needle is not found, strpos () will return the boolean FALSE.

If you are just checking to see if your string has a certain string you should use strpos($minhaStr, $checkStr) !== false , since the string can exist in the first position 0 , so it would not return the desired one in:

if(strpos($minhaStr, $checkStr))   // se a string existir em 0 não entrara no if, pois 0 é considerado false.
if(strpos($minhaStr, $checkStr) > 0)
    
19.10.2015 / 17:50
1

Just one detail to stay 100%, your condition should be changed to:

if (strpos($string, 'comprar-toyota'))

and not:

if (strpos($string, 'comprar/toyota') > 0)

The problem is exactly in the return of the function as specified in the php documentation:

  

If needle is not found, strpos () will return the boolean FALSE.

Example 1:

$string = 'www.loja.com.br/comprar-toyota';

 if (strpos($string, 'comprar-toyota') > 0) {
      // funcionará.
}

Example 2:

$string = 'comprar-toyota';

 if (strpos($string, 'comprar-toyota') > 0) {
      // não funcionará.
}
    
19.10.2015 / 17:58