0

I want to search the string which contains Rathi 25mm but I don't want to use the complete word to search .How can i do the search using specific words?

Rathi Stelmax 500 25 mm in stripos

   <?php
    $title='Rathi Stelmax 500 25 mm';

    if (stripos(strtolower($title), 'Rathi  25 mm') !== false)
    { 
      echo 'true';
    }
    ?>
chris85
  • 23,846
  • 7
  • 34
  • 51
Aryan Kanwar
  • 39
  • 1
  • 1
  • 6

4 Answers4

0

You can try this script

 $title="Rathi Stelmax 500 25 mm";
if(preg_match("/(Rathi|25|mm)/i", $title)){
  //one of these string found
}
Arun sankar
  • 94
  • 1
  • 2
  • 17
0

The best way would be to use preg_match_all.

Thats a REGEX function. Here is a small example:

$input_lines = 'Hello i am 250 years old from Mars'
preg_match_all("/(\d+)|(Mars)/", $input_lines, $output_array);

$output_array would hold an array with the following data:

0   =>  array(2
    0   =>  250
    1   =>  Mars
    )
0

If you are reluctant to use regexp, you can split your search string in words, and check that each of them is contained in the string :

$title = 'Rathi Stelmax 500 25 mm';
$lookFor = 'Rathi 25 mm';

$counter = 0;
$searchElements = explode(' ', $lookFor);
foreach($searchElements as $search){
    if(strpos($title, $search) !== false){
        $counter++;
    }
}

if(count($searchElements) == $counter){
    echo 'found everything';
}
else{
    echo 'found ' . $counter . ' element(s)';
}

It's probably less efficient than a regexp, but also probably easier to grasp.

vincenth
  • 1,732
  • 5
  • 19
  • 27
0

There are a few ways to do this. Using your current approach you can run multiple striposs in the conditional to confirm each word is there:

$title='Rathi Stelmax 500 25 mm';
if (stripos(strtolower($title), 'Rathi') !== false && stripos(strtolower($title),  '25 mm'))
    { echo 'true';
    }

Demo: https://eval.in/628147

You also could use a regex such as:

/Rathi.*25 mm/

PHP Demo: https://eval.in/628148
Regex Demo: https://regex101.com/r/cZ6bL1/1

PHP Usage:

$title='Rathi Stelmax 500 25 mm';
if (preg_match('/Rathi.*25 mm/', $title)) { 
     echo 'true';
}
chris85
  • 23,846
  • 7
  • 34
  • 51