How to search for word snippet within XML using PHP

1

I use this code that is to return data for each searched name, but the query only works if I type a name identically, I wanted to type "Smartphone" and it returns all searches that have this word 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";
    }
}
?>
    
asked by anonymous 23.10.2018 / 20:42

1 answer

2

You can use the strpos native function:

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 look for the word inside the string, and if it does not find anything it returns false. So any result other than false means that you have found some result.

    
23.10.2018 / 21:42