如何使用strpos找到完全匹配?

时间:2018-06-04 02:24:06

标签: php

如果我查看war,如果我查看Award,我会发现 $title = "Music award"; if (strpos($title, 'war') !== false) { echo "found"; }

war

如果我查找award,我不想找到{{1}}

2 个答案:

答案 0 :(得分:1)

使用正则表达式匹配

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

答案 1 :(得分:0)

<?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));

输出:

bool(true)
bool(false)