Search for a word in a JSON result

3

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

Problem 1 happens:

  • The search is only correct when we put in UPPERCASE, because the result of the jSON is upper case.
asked by anonymous 31.07.2015 / 22:13

1 answer

3

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

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

strpos looks for the first index of a string, if it does not find it returns false.

    
31.07.2015 / 22:17