How to search word snippet within XML using PHP

Asked

Viewed 136 times

1

I use this code that is to return data related to each searched name, but the query only works if I type a name identica, I wanted to type "Smartphone" and it returns me all the searches that have this flood in the title. Can you help me?

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" >
Nome <input type="text" name="nome" value="" />
<input type="submit" name="submit" />
</form>
<?php
if (isset($_POST['submit'])) {   
   $xml = simplexml_load_file('https://actionpay.net/br-pt/couponFeed/xmlFilter/id:6607');
    $erro = 0;
    foreach ($xml->promotion AS $val) {
        if ($val->title == $_POST['nome']) {
            echo "<div class='single-product'>
                           <div class='product-f-image'>

                              <div class='product-hover'>
                                 <a href='$val->offer_link' class='add-to-cart-link' target='_blank'><img src='$val->logo' alt=''>$val->title</a>
                              </div>
                           </div>

                           <div class='product-carousel-price'>
                              <del></del>  
                           </div>
                        </div>";
            $erro++;
        }
    }
    if ($erro == 0) {
        echo "nenhum valor encontrado";
    }
}
?>

1 answer

2


You can use the native function strpos:

if (strpos($val->title, $_POST['nome']) !== false) {
   [seu codigo]
}

strpos

(PHP 4, PHP 5, PHP 7)

strpos - Find the position of the first occurrence of a string

It will search for the word inside the string, and if it does not find anything it returns false. So any result other than false means you have found some result.

  • Perfect, thank you very much brother.

  • @Walnut if it worked marks the answer as right to help anyone with the same doubt. Vlw

Browser other questions tagged

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