Find a word in a JSON result

Asked

Viewed 606 times

3

Guys I got the following json:

"results": {
"collection1": [
  {
    "prod": {
      "text": "COLCHÃO NAUTIKA KING SIZE"

I have a search form:

<form id="search" method="POST">
<input type="text" name="search" />
          <input type="submit" value="buscar">
          </form>

And to show the result I did the following:

$search = $_POST['search'];    
foreach($results['results']['collection1'] as $collection) {

                if(strpos($collection['prod']['text'],$search) !== false) {
                echo $collection['prod']['text'] . "'><br />";  }

It happens 1 problem:

  • The search is only right when put in UPPERCASE letter, because the result of json is uppercase.

1 answer

3

Change the code of if, instead of using == use the function strpos.

if(strpos($collection['prod']['text'], "COLCHÃO")) !== false)

strpos searches for the first index of a string, if not find returns false.

  • 1

    Cool! Just fixing an error: if(strpos($Collection['Prod']['text'], "MATTRESS") !== false)

  • I had done it right after I edited and put an extra one. Missed to give compilation error!

Browser other questions tagged

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