rob.m
rob.m

Reputation: 10571

How to find exact match using strpos?

If I look out for war that would also be found if I check Award:

 $title = "Music award";
 if (strpos($title, 'war') !== false) {
    echo "found";
  }

If I look for war I don't want to find award

Upvotes: 0

Views: 1387

Answers (2)

Progrock
Progrock

Reputation: 7485

<?php
$string = "We are the music makers and we are the dreamers of dreams.";
$words  = str_word_count($string, 1);
var_dump(in_array('music', $words));
var_dump(in_array('dream', $words));

Output:

bool(true)
bool(false)

Upvotes: 0

zavg
zavg

Reputation: 11061

Use regex matching

$title = "Music award";
if (preg_match('/\bwar\b/', $title) !== false) {
    echo "found";
}

Upvotes: 1

Related Questions