easyquestions
easyquestions

Reputation: 135

How can I check a haystack string for two separate needle strings?

I'm stuck on this:

I run quite a few strpos in my code, and all of them work with 1 value. But when I need to use 2 values, I cannot get the code working.

Here is the issue, this works with 1 value, but not 2:

<?php 

$a = strtolower($title); 

if (strpos($a, 'opel') !== false) { 
    echo '<h2>Why Opel?</h2>'; 
} 

?>

But I cannot find the right code to work with for example 'opel' 'corsa'

Upvotes: 1

Views: 55

Answers (1)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167250

Why not use a conjunction?

<?php
  $a = strtolower($title);
  if (strpos($a, 'opel') !== false && strpos($a, 'corsa') !== false) { 
    echo '<h2>Why Opel Corsa?</h2>';
  }
?>

Like this?

Upvotes: 1

Related Questions