How to get only strings containing the word "x"

Asked

Viewed 76 times

0

I’m picking up through a parser with PHP DOM all the links on a page. I need to pick up 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 reversed the strpos condition on my staying so:

//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 it’s pulling all the links from the page...I needed to get only the ones that have the string "buy-Toyota" anywhere in the link

How can I fix this?

3 answers

2


Substitute

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

FOR

// 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>";
}
  • Thank you so much beast! It was accurate...

  • 1

    Contemplates the desired by AP, but is not 100% correct.

  • 1

    @Guilhermelautert, Voce was right. The worst I had used type comparison in the test I did, but when I posted the answer I used > 0 .

1

As it says in the manual on strpos:

Returns the numerical position of the first occurrence of Needle within haystack.
If Needle is not found, strpos() will return Boolean FALSE.

If you’re just checking your string has a certain string should be used strpos($minhaStr, $checkStr) !== false, because the string can exist at the first position 0, so would not return the desired 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)

1

Only one detail to get 100%, your condition should be modified 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 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á.
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.