如何检查字符串是否包含特定单词?

发布于 2024-10-06 13:46:53 字数 171 浏览 6 评论 0原文

考虑一下:

$a = 'How are you?';

if ($a contains 'are')
    echo 'true';

假设我有上面的代码,那么 if ($a contains 'are') 语句的正确编写方式是什么?

Consider:

$a = 'How are you?';

if ($a contains 'are')
    echo 'true';

Suppose I have the code above, what is the correct way to write the statement if ($a contains 'are')?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

辞取 2024-10-13 13:46:53

现在,在 PHP 8 中,您可以使用 str_contains 来执行此操作:< /strong>

if (str_contains('How are you', 'are')) { 
    echo 'true';
}

请注意:如果 $needle(在字符串中搜索的子字符串)为空,str_contains 函数将始终返回 true。

$haystack = 'Hello';
$needle   = '';

if (str_contains($haystack, $needle)) {
    echo "This returned true!";
}

您应该首先确保 $needle (您的子字符串)不为空。

$haystack = 'How are you?';
$needle   = '';

if ($needle !== '' && str_contains($haystack, $needle)) {
    echo "This returned true!";
} else {
    echo "This returned false!";
}

输出This returned false!

还值得注意的是,新的str_contains 函数区分大小写。

$haystack = 'How are you?';
$needle   = 'how';

if ($needle !== '' && str_contains($haystack, $needle)) {
    echo "This returned true!";
} else {
    echo "This returned false!";
}

输出返回 false!

RFC

在 PHP 8 之前

您可以使用 strpos()< /code>函数,用于查找一个字符串在另一个字符串中的出现情况:

$haystack = 'How are you?';
$needle   = 'are';

if (strpos($haystack, $needle) !== false) {
    echo 'true';
}

请注意,使用 !== false 是故意的(!= false 都不是故意的) code> 或 === true 都不会返回所需的结果); strpos() 返回针字符串在 haystack 字符串中开始的偏移量,如果未找到针,则返回布尔值 false。由于 0 是有效偏移量,而 0 是“假”,因此我们不能使用像 !strpos($a, 'are') 这样更简单的结构。

Now with PHP 8 you can do this using str_contains:

if (str_contains('How are you', 'are')) { 
    echo 'true';
}

Please note: The str_contains function will always return true if the $needle (the substring to search for in your string) is empty.

$haystack = 'Hello';
$needle   = '';

if (str_contains($haystack, $needle)) {
    echo "This returned true!";
}

You should first make sure the $needle (your substring) is not empty.

$haystack = 'How are you?';
$needle   = '';

if ($needle !== '' && str_contains($haystack, $needle)) {
    echo "This returned true!";
} else {
    echo "This returned false!";
}

Output: This returned false!

It's also worth noting that the new str_contains function is case-sensitive.

$haystack = 'How are you?';
$needle   = 'how';

if ($needle !== '' && str_contains($haystack, $needle)) {
    echo "This returned true!";
} else {
    echo "This returned false!";
}

Output: This returned false!

RFC

Before PHP 8

You can use the strpos() function which is used to find the occurrence of one string inside another one:

$haystack = 'How are you?';
$needle   = 'are';

if (strpos($haystack, $needle) !== false) {
    echo 'true';
}

Note that the use of !== false is deliberate (neither != false nor === true will return the desired result); strpos() returns either the offset at which the needle string begins in the haystack string, or the boolean false if the needle isn't found. Since 0 is a valid offset and 0 is "falsey", we can't use simpler constructs like !strpos($a, 'are').

迎风吟唱 2024-10-13 13:46:53

正如其他用户所提到的,您可以使用正则表达式,因为与 strpos 相比,它更适合单词匹配。对 arestrpos 检查也会对诸如 fare、care、stare 等字符串返回 true。通过使用字边界,可以在正则表达式中简单地避免这些意外匹配。

are 的简单匹配可能如下所示:

$a = 'How are you?';

if (preg_match('/\bare\b/', $a)) {
    echo 'true';
}

在性能方面,strpos 大约快三倍。当我一次进行一百万次比较时,需要 preg_match需要 1.5 秒才能完成,而 strpos 需要 0.5 秒。

编辑:
为了搜索字符串的任何部分,而不仅仅是逐字搜索,我建议使用正则表达式,例如

$a = 'How are you?';
$search = 'are y';
if(preg_match("/{$search}/i", $a)) {
    echo 'true';
}

正则表达式末尾的 i 将正则表达式更改为不区分大小写,如果您不希望这样,您可以将其省略。

现在,在某些情况下这可能会产生很大的问题,因为 $search 字符串没有以任何方式进行清理,我的意思是,在某些情况下它可能无法通过检查,就好像 $search 是用户输入一样他们可以添加一些可能表现得像不同正则表达式的字符串...

另外,这里有一个很棒的工具,用于测试和查看各种正则表达式的解释 Regex101

要将两组功能组合成一个多用途函数(包括可选择区分大小写),您可以使用如下内容:

function FindString($needle,$haystack,$i,$word)
{   // $i should be "" or "i" for case insensitive
    if (strtoupper($word)=="W")
    {   // if $word is "W" then word search instead of string in string search.
        if (preg_match("/\b{$needle}\b/{$i}", $haystack)) 
        {
            return true;
        }
    }
    else
    {
        if(preg_match("/{$needle}/{$i}", $haystack)) 
        {
            return true;
        }
    }
    return false;
    // Put quotes around true and false above to return them as strings instead of as bools/ints.
}

还要记住的一点是 \b 不适用于英语以外的其他语言。

对此的解释和解决方案取自此处

\b 表示单词的开头或结尾(单词边界)。这
正则表达式会匹配苹果派中的苹果,但不会匹配苹果中的苹果
菠萝、苹果车或烤苹果。

“咖啡馆”怎么样?我们如何在正则表达式中提取单词“café”?
实际上,\bcafé\b 不起作用。为什么?因为“咖啡馆”包含
非 ASCII 字符:é。 \b 不能简单地与 Unicode 一起使用,例如
समुद्र、감사、месяц和

You could use regular expressions as it's better for word matching compared to strpos, as mentioned by other users. A strpos check for are will also return true for strings such as: fare, care, stare, etc. These unintended matches can simply be avoided in regular expression by using word boundaries.

A simple match for are could look something like this:

$a = 'How are you?';

if (preg_match('/\bare\b/', $a)) {
    echo 'true';
}

On the performance side, strpos is about three times faster. When I did one million compares at once, it took preg_match 1.5 seconds to finish and for strpos it took 0.5 seconds.

Edit:
In order to search any part of the string, not just word by word, I would recommend using a regular expression like

$a = 'How are you?';
$search = 'are y';
if(preg_match("/{$search}/i", $a)) {
    echo 'true';
}

The i at the end of regular expression changes regular expression to be case-insensitive, if you do not want that, you can leave it out.

Now, this can be quite problematic in some cases as the $search string isn't sanitized in any way, I mean, it might not pass the check in some cases as if $search is a user input they can add some string that might behave like some different regular expression...

Also, here's a great tool for testing and seeing explanations of various regular expressions Regex101

To combine both sets of functionality into a single multi-purpose function (including with selectable case sensitivity), you could use something like this:

function FindString($needle,$haystack,$i,$word)
{   // $i should be "" or "i" for case insensitive
    if (strtoupper($word)=="W")
    {   // if $word is "W" then word search instead of string in string search.
        if (preg_match("/\b{$needle}\b/{$i}", $haystack)) 
        {
            return true;
        }
    }
    else
    {
        if(preg_match("/{$needle}/{$i}", $haystack)) 
        {
            return true;
        }
    }
    return false;
    // Put quotes around true and false above to return them as strings instead of as bools/ints.
}

One more thing to take in mind, is that \b will not work in different languages other than english.

The explanation for this and the solution is taken from here:

\b represents the beginning or end of a word (Word Boundary). This
regex would match apple in an apple pie, but wouldn’t match apple in
pineapple, applecarts or bakeapples.

How about “café”? How can we extract the word “café” in regex?
Actually, \bcafé\b wouldn’t work. Why? Because “café” contains
non-ASCII character: é. \b can’t be simply used with Unicode such as
समुद्र, 감사, месяц and ???? .

When you want to extract Unicode characters, you should directly
define characters which represent word boundaries.

The answer: (?<=[\s,.:;"']|^)UNICODE_WORD(?=[\s,.:;"']|$)

So in order to use the answer in PHP, you can use this function:

function contains($str, array $arr) {
    // Works in Hebrew and any other unicode characters
    // Thanks https://medium.com/@shiba1014/regex-word-boundaries-with-unicode-207794f6e7ed
    // Thanks https://www.phpliveregex.com/
    if (preg_match('/(?<=[\s,.:;"\']|^)' . $word . '(?=[\s,.:;"\']|$)/', $str)) return true;
}

And if you want to search for array of words, you can use this:

function arrayContainsWord($str, array $arr)
{
    foreach ($arr as $word) {
        // Works in Hebrew and any other unicode characters
        // Thanks https://medium.com/@shiba1014/regex-word-boundaries-with-unicode-207794f6e7ed
        // Thanks https://www.phpliveregex.com/
        if (preg_match('/(?<=[\s,.:;"\']|^)' . $word . '(?=[\s,.:;"\']|$)/', $str)) return true;
    }
    return false;
}

As of PHP 8.0.0 you can now use str_contains

<?php
    if (str_contains('abc', '')) {
        echo "Checking the existence of the empty string will always"
        return true;
    }
谁的年少不轻狂 2024-10-13 13:46:53

这是一个在这种情况下很有用的小实用函数

// returns true if $needle is a substring of $haystack
function contains($needle, $haystack)
{
    return strpos($haystack, $needle) !== false;
}

Here is a little utility function that is useful in situations like this

// returns true if $needle is a substring of $haystack
function contains($needle, $haystack)
{
    return strpos($haystack, $needle) !== false;
}
无远思近则忧 2024-10-13 13:46:53

要确定一个字符串是否包含另一个字符串,您可以使用 PHP 函数 strpos()

int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )`
<?php

$haystack = 'how are you';
$needle = 'are';

if (strpos($haystack,$needle) !== false) {
    echo "$haystack contains $needle";
}

?>

注意:

如果您要搜索的针位于大海捞针的开头,它将返回位置 0,如果您执行的 == 比较不起作用,您将需要做一个 ===

== 符号是一个比较,测试左边的变量/表达式/常量是否与变量/表达式/具有相同的值向右不变。

=== 符号是比较,以查看两个变量/表达式/常量是否相等AND 具有相同的类型 - 即都是字符串或都是整数。

使用此方法的优点之一是每个 PHP 版本都支持此函数,这与 str_contains() 不同。

To determine whether a string contains another string you can use the PHP function strpos().

int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )`
<?php

$haystack = 'how are you';
$needle = 'are';

if (strpos($haystack,$needle) !== false) {
    echo "$haystack contains $needle";
}

?>

CAUTION:

If the needle you are searching for is at the beginning of the haystack it will return position 0, if you do a == compare that will not work, you will need to do a ===

A == sign is a comparison and tests whether the variable / expression / constant to the left has the same value as the variable / expression / constant to the right.

A === sign is a comparison to see whether two variables / expresions / constants are equal AND have the same type - i.e. both are strings or both are integers.

One of the advantages of using this approach is that every PHP version supports this function, unlike str_contains().

懵少女 2024-10-13 13:46:53

虽然大多数答案都会告诉您子字符串是否出现在字符串中,但如果您要查找特定的单词,而不是子字符串,那么这通常不是您想要的。

有什么区别?子字符串可以出现在其他单词中:

  • “area”位于“area”开头
  • “are”位于“hare”末尾
  • “are”位于“fares”中间

缓解这种情况的一种方法是使用正则表达式与 单词边界 (\b) 相结合

function containsWord($str, $word)
{
    return !!preg_match('#\\b' . preg_quote($word, '#') . '\\b#i', $str);
}

:该方法没有上述相同的误报,但它确实有一些自己的边缘情况。单词边界匹配非单词字符 (\W),这些字符不是 azAZ0-9_。这意味着数字和下划线将被计为单词字符,这样的场景将会失败:

  • “你在想什么?”中的“are”。
  • “哈哈,你不知道那些是4吗?”中的“是”

如果你想要比这更准确的东西,你就必须开始进行英语语法解析,这是一个相当大的蠕虫罐头(并且假设正确使用语法,无论如何,这并不总是给定的)。

While most of these answers will tell you if a substring appears in your string, that's usually not what you want if you're looking for a particular word, and not a substring.

What's the difference? Substrings can appear within other words:

  • The "are" at the beginning of "area"
  • The "are" at the end of "hare"
  • The "are" in the middle of "fares"

One way to mitigate this would be to use a regular expression coupled with word boundaries (\b):

function containsWord($str, $word)
{
    return !!preg_match('#\\b' . preg_quote($word, '#') . '\\b#i', $str);
}

This method doesn't have the same false positives noted above, but it does have some edge cases of its own. Word boundaries match on non-word characters (\W), which are going to be anything that isn't a-z, A-Z, 0-9, or _. That means digits and underscores are going to be counted as word characters and scenarios like this will fail:

  • The "are" in "What _are_ you thinking?"
  • The "are" in "lol u dunno wut those are4?"

If you want anything more accurate than this, you'll have to start doing English language syntax parsing, and that's a pretty big can of worms (and assumes proper use of syntax, anyway, which isn't always a given).

独闯女儿国 2024-10-13 13:46:53

查看 strpos()

<?php
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

// Note our use of ===. Simply, == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'.";
} else {
    echo "The string '$findme' was found in the string '$mystring',";
    echo " and exists at position $pos.";
}

Look at strpos():

<?php
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

// Note our use of ===. Simply, == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'.";
} else {
    echo "The string '$findme' was found in the string '$mystring',";
    echo " and exists at position $pos.";
}
淡水深流 2024-10-13 13:46:53

使用 strstr()stristr() 如果您的搜索不区分大小写,则另一种选择。

Using strstr() or stristr() if your search should be case insensitive would be another option.

披肩女神 2024-10-13 13:46:53

同行 SamGoody 和 Lego Stormtroopr 评论。

如果您正在寻找一种 PHP 算法来根据多个单词的邻近度/相关性对搜索结果进行排名
这里有一种仅使用 PHP 生成搜索结果的快速简便的方法:

其他布尔搜索方法的问题,例如 strpos()preg_match()、< code>strstr() 或 stristr()

  1. 无法搜索多个单词
  2. 结果未排名

基于 矢量空间模型tf-idf(词频-逆文档频率):

听起来很难,但出奇的简单。

如果我们要搜索字符串中的多个单词,核心问题是如何为每个单词分配权重?

如果我们可以根据字符串中的术语在整个字符串中的代表性来对它们进行加权,
我们可以按照与查询最匹配的结果对结果进行排序。

这就是向量空间模型的思想,与 SQL 全文搜索的工作方式相差不远:

function get_corpus_index($corpus = array(), $separator=' ') {

    $dictionary = array();

    $doc_count = array();

    foreach($corpus as $doc_id => $doc) {

        $terms = explode($separator, $doc);

        $doc_count[$doc_id] = count($terms);

        // tf–idf, short for term frequency–inverse document frequency, 
        // according to wikipedia is a numerical statistic that is intended to reflect 
        // how important a word is to a document in a corpus

        foreach($terms as $term) {

            if(!isset($dictionary[$term])) {

                $dictionary[$term] = array('document_frequency' => 0, 'postings' => array());
            }
            if(!isset($dictionary[$term]['postings'][$doc_id])) {

                $dictionary[$term]['document_frequency']++;

                $dictionary[$term]['postings'][$doc_id] = array('term_frequency' => 0);
            }

            $dictionary[$term]['postings'][$doc_id]['term_frequency']++;
        }

        //from http://phpir.com/simple-search-the-vector-space-model/

    }

    return array('doc_count' => $doc_count, 'dictionary' => $dictionary);
}

function get_similar_documents($query='', $corpus=array(), $separator=' '){

    $similar_documents=array();

    if($query!=''&&!empty($corpus)){

        $words=explode($separator,$query);

        $corpus=get_corpus_index($corpus, $separator);

        $doc_count=count($corpus['doc_count']);

        foreach($words as $word) {

            if(isset($corpus['dictionary'][$word])){

                $entry = $corpus['dictionary'][$word];


                foreach($entry['postings'] as $doc_id => $posting) {

                    //get term frequency–inverse document frequency
                    $score=$posting['term_frequency'] * log($doc_count + 1 / $entry['document_frequency'] + 1, 2);

                    if(isset($similar_documents[$doc_id])){

                        $similar_documents[$doc_id]+=$score;

                    }
                    else{

                        $similar_documents[$doc_id]=$score;

                    }
                }
            }
        }

        // length normalise
        foreach($similar_documents as $doc_id => $score) {

            $similar_documents[$doc_id] = $score/$corpus['doc_count'][$doc_id];

        }

        // sort from  high to low

        arsort($similar_documents);

    }   

    return $similar_documents;
}

CASE 1

$query = 'are';

$corpus = array(
    1 => 'How are you?',
);

$match_results=get_similar_documents($query,$corpus);
echo '<pre>';
    print_r($match_results);
echo '</pre>';

RESULT

Array
(
    [1] => 0.52832083357372
)

CASE 2

$query = 'are';

$corpus = array(
    1 => 'how are you today?',
    2 => 'how do you do',
    3 => 'here you are! how are you? Are we done yet?'
);

$match_results=get_similar_documents($query,$corpus);
echo '<pre>';
    print_r($match_results);
echo '</pre>';

结果

Array
(
    [1] => 0.54248125036058
    [3] => 0.21699250014423
)

案例3

$query = 'we are done';

$corpus = array(
    1 => 'how are you today?',
    2 => 'how do you do',
    3 => 'here you are! how are you? Are we done yet?'
);

$match_results=get_similar_documents($query,$corpus);
echo '<pre>';
    print_r($match_results);
echo '</pre>';

结果

Array
(
    [3] => 0.6813781191217
    [1] => 0.54248125036058
)

还有很多需要改进的地方
但该模型提供了一种从自然查询中获得良好结果的方法,
没有布尔运算符,例如 strpos()preg_match()strstr()stristr().

NOTA BENE

可以选择在搜索单词之前消除冗余

  • 从而减小索引大小并减少存储需求

  • 更少的磁盘I/O

  • 更快的索引和因此更快的搜索。

1.规范化

  • 将所有文本转换为小写

2.停用词消除

  • 消除文本中没有实际意义的单词(如“and”、“or”、“the”、“for”等)

3.字典替换

  • 用具有相同或相似含义的其他单词替换单词。
    (例如:将“饥饿”和“饥饿”的实例替换为“饥饿”)

  • 可以执行进一步的算法措施(滚雪球)以进一步将单词减少到其基本含义。

  • 替换颜色名称

  • 通过降低精度来减少数值是标准化文本的其他方法。

资源

Peer to SamGoody and Lego Stormtroopr comments.

If you are looking for a PHP algorithm to rank search results based on proximity/relevance of multiple words
here comes a quick and easy way of generating search results with PHP only:

Issues with the other boolean search methods such as strpos(), preg_match(), strstr() or stristr()

  1. can't search for multiple words
  2. results are unranked

PHP method based on Vector Space Model and tf-idf (term frequency–inverse document frequency):

It sounds difficult but is surprisingly easy.

If we want to search for multiple words in a string the core problem is how we assign a weight to each one of them?

If we could weight the terms in a string based on how representative they are of the string as a whole,
we could order our results by the ones that best match the query.

This is the idea of the vector space model, not far from how SQL full-text search works:

function get_corpus_index($corpus = array(), $separator=' ') {

    $dictionary = array();

    $doc_count = array();

    foreach($corpus as $doc_id => $doc) {

        $terms = explode($separator, $doc);

        $doc_count[$doc_id] = count($terms);

        // tf–idf, short for term frequency–inverse document frequency, 
        // according to wikipedia is a numerical statistic that is intended to reflect 
        // how important a word is to a document in a corpus

        foreach($terms as $term) {

            if(!isset($dictionary[$term])) {

                $dictionary[$term] = array('document_frequency' => 0, 'postings' => array());
            }
            if(!isset($dictionary[$term]['postings'][$doc_id])) {

                $dictionary[$term]['document_frequency']++;

                $dictionary[$term]['postings'][$doc_id] = array('term_frequency' => 0);
            }

            $dictionary[$term]['postings'][$doc_id]['term_frequency']++;
        }

        //from http://phpir.com/simple-search-the-vector-space-model/

    }

    return array('doc_count' => $doc_count, 'dictionary' => $dictionary);
}

function get_similar_documents($query='', $corpus=array(), $separator=' '){

    $similar_documents=array();

    if($query!=''&&!empty($corpus)){

        $words=explode($separator,$query);

        $corpus=get_corpus_index($corpus, $separator);

        $doc_count=count($corpus['doc_count']);

        foreach($words as $word) {

            if(isset($corpus['dictionary'][$word])){

                $entry = $corpus['dictionary'][$word];


                foreach($entry['postings'] as $doc_id => $posting) {

                    //get term frequency–inverse document frequency
                    $score=$posting['term_frequency'] * log($doc_count + 1 / $entry['document_frequency'] + 1, 2);

                    if(isset($similar_documents[$doc_id])){

                        $similar_documents[$doc_id]+=$score;

                    }
                    else{

                        $similar_documents[$doc_id]=$score;

                    }
                }
            }
        }

        // length normalise
        foreach($similar_documents as $doc_id => $score) {

            $similar_documents[$doc_id] = $score/$corpus['doc_count'][$doc_id];

        }

        // sort from  high to low

        arsort($similar_documents);

    }   

    return $similar_documents;
}

CASE 1

$query = 'are';

$corpus = array(
    1 => 'How are you?',
);

$match_results=get_similar_documents($query,$corpus);
echo '<pre>';
    print_r($match_results);
echo '</pre>';

RESULT

Array
(
    [1] => 0.52832083357372
)

CASE 2

$query = 'are';

$corpus = array(
    1 => 'how are you today?',
    2 => 'how do you do',
    3 => 'here you are! how are you? Are we done yet?'
);

$match_results=get_similar_documents($query,$corpus);
echo '<pre>';
    print_r($match_results);
echo '</pre>';

RESULTS

Array
(
    [1] => 0.54248125036058
    [3] => 0.21699250014423
)

CASE 3

$query = 'we are done';

$corpus = array(
    1 => 'how are you today?',
    2 => 'how do you do',
    3 => 'here you are! how are you? Are we done yet?'
);

$match_results=get_similar_documents($query,$corpus);
echo '<pre>';
    print_r($match_results);
echo '</pre>';

RESULTS

Array
(
    [3] => 0.6813781191217
    [1] => 0.54248125036058
)

There are plenty of improvements to be made
but the model provides a way of getting good results from natural queries,
which don't have boolean operators such as strpos(), preg_match(), strstr() or stristr().

NOTA BENE

Optionally eliminating redundancy prior to search the words

  • thereby reducing index size and resulting in less storage requirement

  • less disk I/O

  • faster indexing and a consequently faster search.

1. Normalisation

  • Convert all text to lower case

2. Stopword elimination

  • Eliminate words from the text which carry no real meaning (like 'and', 'or', 'the', 'for', etc.)

3. Dictionary substitution

  • Replace words with others which have an identical or similar meaning.
    (ex:replace instances of 'hungrily' and 'hungry' with 'hunger')

  • Further algorithmic measures (snowball) may be performed to further reduce words to their essential meaning.

  • The replacement of colour names with their hexadecimal equivalents

  • The reduction of numeric values by reducing precision are other ways of normalising the text.

RESOURCES

迷荒 2024-10-13 13:46:53

使用 strpos() 进行子字符串匹配:

if (strpos($string,$stringToSearch) !== false) {
    echo 'true';
}

Make use of substring matching using strpos():

if (strpos($string,$stringToSearch) !== false) {
    echo 'true';
}
云归处 2024-10-13 13:46:53

如果你想避免“假”和“真”问题,可以使用 substr_count:

if (substr_count($a, 'are') > 0) {
    echo "at least one 'are' is present!";
}

它比 strpos 慢一点,但它避免了比较问题。

If you want to avoid the "falsey" and "truthy" problem, you can use substr_count:

if (substr_count($a, 'are') > 0) {
    echo "at least one 'are' is present!";
}

It's a bit slower than strpos but it avoids the comparison problems.

想你的星星会说话 2024-10-13 13:46:53
if (preg_match('/(are)/', $a)) {
   echo 'true';
}
if (preg_match('/(are)/', $a)) {
   echo 'true';
}
甩你一脸翔 2024-10-13 13:46:53

另一种选择是使用 strstr() 函数。类似于:

if (strlen(strstr($haystack,$needle))>0) {
// Needle Found
}

需要注意的是: strstr() 函数区分大小写。对于不区分大小写的搜索,请使用 stristr() 函数。

Another option is to use the strstr() function. Something like:

if (strlen(strstr($haystack,$needle))>0) {
// Needle Found
}

Point to note: The strstr() function is case-sensitive. For a case-insensitive search, use the stristr() function.

虐人心 2024-10-13 13:46:53

令我印象深刻的是,这里没有使用 strposstrstr 和类似函数的答案多字节字符串函数 (2015-05-08)。

基本上,如果您无法找到包含某些语言特有字符的单词,例如德语、法语、葡萄牙语、西班牙语等(例如:äéôçºñ),您可能需要在具有 mb_ 的功能。因此,接受的答案将使用 mb_strposmb_stripos (用于不区分大小写的匹配) 相反:

if (mb_strpos($a,'are') !== false) {
    echo 'true';
}

如果您不能保证所有数据都是 100% 采用 UTF-8,您可能想使用 mb_ 函数。

一篇很好的文章,可以帮助您理解为什么 每个软件开发人员绝对必须了解 Unicode和字符集(没有借口!) 作者:Joel Spolsky

I'm a bit impressed that none of the answers here that used strpos, strstr and similar functions mentioned Multibyte String Functions yet (2015-05-08).

Basically, if you're having trouble finding words with characters specific to some languages, such as German, French, Portuguese, Spanish, etc. (e.g.: ä, é, ô, ç, º, ñ), you may want to precede the functions with mb_. Therefore, the accepted answer would use mb_strpos or mb_stripos (for case-insensitive matching) instead:

if (mb_strpos($a,'are') !== false) {
    echo 'true';
}

If you cannot guarantee that all your data is 100% in UTF-8, you may want to use the mb_ functions.

A good article to understand why is The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) by Joel Spolsky.

满地尘埃落定 2024-10-13 13:46:53

在 PHP 中,验证字符串是否包含某个子字符串的最佳方法是使用一个简单的辅助函数,如下所示:

function contains($haystack, $needle, $caseSensitive = false) {
    return $caseSensitive ?
            (strpos($haystack, $needle) === FALSE ? FALSE : TRUE):
            (stripos($haystack, $needle) === FALSE ? FALSE : TRUE);
}

说明:

  • strpos 查找字符串中区分大小写的子字符串第一次出现的位置。
  • stripos 查找 a 第一次出现的位置字符串中不区分大小写的子字符串。
  • myFunction($haystack, $needle) === FALSE ? FALSE : TRUE 确保 myFunction 始终返回布尔值并修复子字符串索引为 0 时的意外行为。
  • $caseSensitive ? A : B 选择 strposstripos 来完成工作,具体取决于$caseSensitive 的值。

输出:

var_dump(contains('bare','are'));            // Outputs: bool(true)
var_dump(contains('stare', 'are'));          // Outputs: bool(true)
var_dump(contains('stare', 'Are'));          // Outputs: bool(true)
var_dump(contains('stare', 'Are', true));    // Outputs: bool(false)
var_dump(contains('hair', 'are'));           // Outputs: bool(false)
var_dump(contains('aren\'t', 'are'));        // Outputs: bool(true)
var_dump(contains('Aren\'t', 'are'));        // Outputs: bool(true)
var_dump(contains('Aren\'t', 'are', true));  // Outputs: bool(false)
var_dump(contains('aren\'t', 'Are'));        // Outputs: bool(true)
var_dump(contains('aren\'t', 'Are', true));  // Outputs: bool(false)
var_dump(contains('broad', 'are'));          // Outputs: bool(false)
var_dump(contains('border', 'are'));         // Outputs: bool(false)

In PHP, the best way to verify if a string contains a certain substring, is to use a simple helper function like this:

function contains($haystack, $needle, $caseSensitive = false) {
    return $caseSensitive ?
            (strpos($haystack, $needle) === FALSE ? FALSE : TRUE):
            (stripos($haystack, $needle) === FALSE ? FALSE : TRUE);
}

Explanation:

  • strpos finds the position of the first occurrence of a case-sensitive substring in a string.
  • stripos finds the position of the first occurrence of a case-insensitive substring in a string.
  • myFunction($haystack, $needle) === FALSE ? FALSE : TRUE ensures that myFunction always returns a boolean and fixes unexpected behavior when the index of the substring is 0.
  • $caseSensitive ? A : B selects either strpos or stripos to do the work, depending on the value of $caseSensitive.

Output:

var_dump(contains('bare','are'));            // Outputs: bool(true)
var_dump(contains('stare', 'are'));          // Outputs: bool(true)
var_dump(contains('stare', 'Are'));          // Outputs: bool(true)
var_dump(contains('stare', 'Are', true));    // Outputs: bool(false)
var_dump(contains('hair', 'are'));           // Outputs: bool(false)
var_dump(contains('aren\'t', 'are'));        // Outputs: bool(true)
var_dump(contains('Aren\'t', 'are'));        // Outputs: bool(true)
var_dump(contains('Aren\'t', 'are', true));  // Outputs: bool(false)
var_dump(contains('aren\'t', 'Are'));        // Outputs: bool(true)
var_dump(contains('aren\'t', 'Are', true));  // Outputs: bool(false)
var_dump(contains('broad', 'are'));          // Outputs: bool(false)
var_dump(contains('border', 'are'));         // Outputs: bool(false)
匿名。 2024-10-13 13:46:53

您可以使用 strstr 函数:

$haystack = "I know programming";
$needle   = "know";
$flag = strstr($haystack, $needle);

if ($flag){

    echo "true";
}

不使用内置函数:

$haystack  = "hello world";
$needle = "llo";

$i = $j = 0;

while (isset($needle[$i])) {
    while (isset($haystack[$j]) && ($needle[$i] != $haystack[$j])) {
        $j++;
        $i = 0;
    }
    if (!isset($haystack[$j])) {
        break;
    }
    $i++;
    $j++;

}
if (!isset($needle[$i])) {
    echo "YES";
}
else{
    echo "NO ";
}

You can use the strstr function:

$haystack = "I know programming";
$needle   = "know";
$flag = strstr($haystack, $needle);

if ($flag){

    echo "true";
}

Without using an inbuilt function:

$haystack  = "hello world";
$needle = "llo";

$i = $j = 0;

while (isset($needle[$i])) {
    while (isset($haystack[$j]) && ($needle[$i] != $haystack[$j])) {
        $j++;
        $i = 0;
    }
    if (!isset($haystack[$j])) {
        break;
    }
    $i++;
    $j++;

}
if (!isset($needle[$i])) {
    echo "YES";
}
else{
    echo "NO ";
}
非要怀念 2024-10-13 13:46:53

下面的函数也可以工作,并且不依赖于任何其他函数;它仅使用本机 PHP 字符串操作。就我个人而言,我不推荐这样做,但你可以看看它是如何工作的:

<?php

if (!function_exists('is_str_contain')) {
  function is_str_contain($string, $keyword)
  {
    if (empty($string) || empty($keyword)) return false;
    $keyword_first_char = $keyword[0];
    $keyword_length = strlen($keyword);
    $string_length = strlen($string);

    // case 1
    if ($string_length < $keyword_length) return false;

    // case 2
    if ($string_length == $keyword_length) {
      if ($string == $keyword) return true;
      else return false;
    }

    // case 3
    if ($keyword_length == 1) {
      for ($i = 0; $i < $string_length; $i++) {

        // Check if keyword's first char == string's first char
        if ($keyword_first_char == $string[$i]) {
          return true;
        }
      }
    }

    // case 4
    if ($keyword_length > 1) {
      for ($i = 0; $i < $string_length; $i++) {
        /*
        the remaining part of the string is equal or greater than the keyword
        */
        if (($string_length + 1 - $i) >= $keyword_length) {

          // Check if keyword's first char == string's first char
          if ($keyword_first_char == $string[$i]) {
            $match = 1;
            for ($j = 1; $j < $keyword_length; $j++) {
              if (($i + $j < $string_length) && $keyword[$j] == $string[$i + $j]) {
                $match++;
              }
              else {
                return false;
              }
            }

            if ($match == $keyword_length) {
              return true;
            }

            // end if first match found
          }

          // end if remaining part
        }
        else {
          return false;
        }

        // end for loop
      }

      // end case4
    }

    return false;
  }
}

测试:

var_dump(is_str_contain("test", "t")); //true
var_dump(is_str_contain("test", "")); //false
var_dump(is_str_contain("test", "test")); //true
var_dump(is_str_contain("test", "testa")); //flase
var_dump(is_str_contain("a----z", "a")); //true
var_dump(is_str_contain("a----z", "z")); //true 
var_dump(is_str_contain("mystringss", "strings")); //true 

The function below also works and does not depend on any other function; it uses only native PHP string manipulation. Personally, I do not recommend this, but you can see how it works:

<?php

if (!function_exists('is_str_contain')) {
  function is_str_contain($string, $keyword)
  {
    if (empty($string) || empty($keyword)) return false;
    $keyword_first_char = $keyword[0];
    $keyword_length = strlen($keyword);
    $string_length = strlen($string);

    // case 1
    if ($string_length < $keyword_length) return false;

    // case 2
    if ($string_length == $keyword_length) {
      if ($string == $keyword) return true;
      else return false;
    }

    // case 3
    if ($keyword_length == 1) {
      for ($i = 0; $i < $string_length; $i++) {

        // Check if keyword's first char == string's first char
        if ($keyword_first_char == $string[$i]) {
          return true;
        }
      }
    }

    // case 4
    if ($keyword_length > 1) {
      for ($i = 0; $i < $string_length; $i++) {
        /*
        the remaining part of the string is equal or greater than the keyword
        */
        if (($string_length + 1 - $i) >= $keyword_length) {

          // Check if keyword's first char == string's first char
          if ($keyword_first_char == $string[$i]) {
            $match = 1;
            for ($j = 1; $j < $keyword_length; $j++) {
              if (($i + $j < $string_length) && $keyword[$j] == $string[$i + $j]) {
                $match++;
              }
              else {
                return false;
              }
            }

            if ($match == $keyword_length) {
              return true;
            }

            // end if first match found
          }

          // end if remaining part
        }
        else {
          return false;
        }

        // end for loop
      }

      // end case4
    }

    return false;
  }
}

Test:

var_dump(is_str_contain("test", "t")); //true
var_dump(is_str_contain("test", "")); //false
var_dump(is_str_contain("test", "test")); //true
var_dump(is_str_contain("test", "testa")); //flase
var_dump(is_str_contain("a----z", "a")); //true
var_dump(is_str_contain("a----z", "z")); //true 
var_dump(is_str_contain("mystringss", "strings")); //true 
-小熊_ 2024-10-13 13:46:53

很多答案使用 substr_count 检查是否结果是>0。但由于 if 语句认为零,与 false 相同,您可以避免该检查并直接写入:

if (substr_count($a, 'are')) {

要检查是否存在,请添加 ! 运算符:

if (!substr_count($a, 'are')) {

Lot of answers that use substr_count checks if the result is >0. But since the if statement considers zero the same as false, you can avoid that check and write directly:

if (substr_count($a, 'are')) {

To check if not present, add the ! operator:

if (!substr_count($a, 'are')) {
揽月 2024-10-13 13:46:53

我对此遇到了一些麻烦,最后我选择创建自己的解决方案。不使用正则表达式引擎:

function contains($text, $word)
{
    $found = false;
    $spaceArray = explode(' ', $text);

    $nonBreakingSpaceArray = explode(chr(160), $text);

    if (in_array($word, $spaceArray) ||
        in_array($word, $nonBreakingSpaceArray)
       ) {

        $found = true;
    }
    return $found;
 }

您可能会注意到之前的解决方案并不是这个词的答案用作另一个的前缀。为了使用您的示例:

$a = 'How are you?';
$b = "a skirt that flares from the waist";
$c = "are";

在上面的示例中,$a$b都包含$c,但您可能希望您的函数告诉你只有 $a 包含 $c

I had some trouble with this, and finally I chose to create my own solution. Without using regular expression engine:

function contains($text, $word)
{
    $found = false;
    $spaceArray = explode(' ', $text);

    $nonBreakingSpaceArray = explode(chr(160), $text);

    if (in_array($word, $spaceArray) ||
        in_array($word, $nonBreakingSpaceArray)
       ) {

        $found = true;
    }
    return $found;
 }

You may notice that the previous solutions are not an answer for the word being used as a prefix for another. In order to use your example:

$a = 'How are you?';
$b = "a skirt that flares from the waist";
$c = "are";

With the samples above, both $a and $b contains $c, but you may want your function to tell you that only $a contains $c.

荒芜了季节 2024-10-13 13:46:53

使用 strstr()stristr() 如下所示:

<?php
    $a = 'How are you?';
    if (strstr($a,'are'))  // Case sensitive
        echo 'true';
    if (stristr($a,'are'))  // Case insensitive
        echo 'true';
?>

Another option to finding the occurrence of a word from a string using strstr() and stristr() is like the following:

<?php
    $a = 'How are you?';
    if (strstr($a,'are'))  // Case sensitive
        echo 'true';
    if (stristr($a,'are'))  // Case insensitive
        echo 'true';
?>
久随 2024-10-13 13:46:53

它可以通过三种不同的方式完成:

 $a = 'How are you?';

1- stristr()

 if (strlen(stristr($a,"are"))>0) {
    echo "true"; // are Found
 } 

2- strpos()

 if (strpos($a, "are") !== false) {
   echo "true"; // are Found
 }

3- preg_match()

 if( preg_match("are",$a) === 1) {
   echo "true"; // are Found
 }

It can be done in three different ways:

 $a = 'How are you?';

1- stristr()

 if (strlen(stristr($a,"are"))>0) {
    echo "true"; // are Found
 } 

2- strpos()

 if (strpos($a, "are") !== false) {
   echo "true"; // are Found
 }

3- preg_match()

 if( preg_match("are",$a) === 1) {
   echo "true"; // are Found
 }
佞臣 2024-10-13 13:46:53

如果您只想检查一个字符串是否包含在另一字符串中,请不要使用 preg_match()。请改用 strpos()strstr(),因为它们会更快。 (https://www.php.net/preg_match)

if (strpos($text, 'string_name') !== false){
   echo 'get the string';
}
 

Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster. (https://www.php.net/preg_match)

if (strpos($text, 'string_name') !== false){
   echo 'get the string';
}
 
土豪 2024-10-13 13:46:53

简写版本

$result = false!==strpos($a, 'are');

The short-hand version

$result = false!==strpos($a, 'are');
薆情海 2024-10-13 13:46:53

为了找到一个“单词”,而不是出现一系列实际上可能是另一个单词的一部分的字母,以下将是一个很好的解决方案。

$string = 'How are you?';
$array = explode(" ", $string);

if (in_array('are', $array) ) {
    echo 'Found the word';
}

In order to find a 'word', rather than the occurrence of a series of letters that could in fact be a part of another word, the following would be a good solution.

$string = 'How are you?';
$array = explode(" ", $string);

if (in_array('are', $array) ) {
    echo 'Found the word';
}
国粹 2024-10-13 13:46:53

您应该使用不区分大小写的格式,因此如果输入的值是 smallcaps 也没关系。

<?php
$grass = "This is pratik joshi";
$needle = "pratik";
if (stripos($grass,$needle) !== false) { 

 /*If i EXCLUDE : !== false then if string is found at 0th location, 
   still it will say STRING NOT FOUND as it will return '0' and it      
   will goto else and will say NOT Found though it is found at 0th location.*/
    echo 'Contains word';
}else{
    echo "does NOT contain word";
}
?>

这里stripos在heystack中找到needle而不考虑大小写(小/大写)。

带输出的 PHP 代码示例

You should use case Insensitive format,so if the entered value is in small or caps it wont matter.

<?php
$grass = "This is pratik joshi";
$needle = "pratik";
if (stripos($grass,$needle) !== false) { 

 /*If i EXCLUDE : !== false then if string is found at 0th location, 
   still it will say STRING NOT FOUND as it will return '0' and it      
   will goto else and will say NOT Found though it is found at 0th location.*/
    echo 'Contains word';
}else{
    echo "does NOT contain word";
}
?>

Here stripos finds needle in heystack without considering case (small/caps).

PHPCode Sample with output

堇年纸鸢 2024-10-13 13:46:53

也许你可以使用这样的东西:

<?php
    findWord('Test all OK');

    function findWord($text) {
        if (strstr($text, 'ok')) {
            echo 'Found a word';
        }
        else
        {
            echo 'Did not find a word';
        }
    }
?>

Maybe you could use something like this:

<?php
    findWord('Test all OK');

    function findWord($text) {
        if (strstr($text, 'ok')) {
            echo 'Found a word';
        }
        else
        {
            echo 'Did not find a word';
        }
    }
?>
泛泛之交 2024-10-13 13:46:53

如果您想检查字符串是否包含多个特定单词,您可以执行以下操作:

$badWords = array("dette", "capitale", "rembourser", "ivoire", "mandat");

$string = "a string with the word ivoire";

$matchFound = preg_match_all("/\b(" . implode($badWords,"|") . ")\b/i", $string, $matches);

if ($matchFound) {
    echo "a bad word has been found";
}
else {
    echo "your string is okay";
}

例如,这对于在发送电子邮件时避免垃圾邮件很有用。

If you want to check if the string contains several specifics words, you can do:

$badWords = array("dette", "capitale", "rembourser", "ivoire", "mandat");

$string = "a string with the word ivoire";

$matchFound = preg_match_all("/\b(" . implode($badWords,"|") . ")\b/i", $string, $matches);

if ($matchFound) {
    echo "a bad word has been found";
}
else {
    echo "your string is okay";
}

This is useful to avoid spam when sending emails for example.

心的位置 2024-10-13 13:46:53

strpos 函数工作正常,但如果您想对段落中的单词进行不区分大小写检查,那么您可以使用 PHP 的 stripos 函数。

例如,

$result = stripos("I love PHP, I love PHP too!", "php");
if ($result === false) {
    // Word does not exist
}
else {
    // Word exists
}

查找字符串中不区分大小写的子字符串第一次出现的位置。

如果字符串中不存在该单词,则返回 false,否则返回该单词的位置。

The strpos function works fine, but if you want to do case-insensitive checking for a word in a paragraph then you can make use of the stripos function of PHP.

For example,

$result = stripos("I love PHP, I love PHP too!", "php");
if ($result === false) {
    // Word does not exist
}
else {
    // Word exists
}

Find the position of the first occurrence of a case-insensitive substring in a string.

If the word doesn't exist in the string then it will return false else it will return the position of the word.

一念一轮回 2024-10-13 13:46:53

可以使用以下函数检查字符串:

function either_String_existor_not($str, $character) {
    return strpos($str, $character) !== false;
}

A string can be checked with the below function:

function either_String_existor_not($str, $character) {
    return strpos($str, $character) !== false;
}
攒一口袋星星 2024-10-13 13:46:53

您需要使用相同/不相同的运算符,因为 strpos 可以返回 0 作为其索引值。如果您喜欢三元运算符,请考虑使用以下内容(我承认似乎有点倒退):

echo FALSE === strpos($a,'are') ? 'false': 'true';

You need to use identical/not identical operators because strpos can return 0 as it's index value. If you like ternary operators, consider using the following (seems a little backwards I'll admit):

echo FALSE === strpos($a,'are') ? 'false': 'true';
掩饰不了的爱 2024-10-13 13:46:53

检查字符串是否包含特定单词?

这意味着必须将字符串解析为单词(请参见下面的注释)。

执行此操作并指定分隔符的一种方法是使用 preg_split (doc):

<?php

function contains_word($str, $word) {
  // split string into words
  // separators are substrings of at least one non-word character
  $arr = preg_split('/\W+/', $str, NULL, PREG_SPLIT_NO_EMPTY);

  // now the words can be examined each
  foreach ($arr as $value) {
    if ($value === $word) {
      return true;
    }
  }
  return false;
}

function test($str, $word) {
  if (contains_word($str, $word)) {
    echo "string '" . $str . "' contains word '" . $word . "'\n";
  } else {
    echo "string '" . $str . "' does not contain word '" . $word . "'\n" ;
  }
}

$a = 'How are you?';

test($a, 'are');
test($a, 'ar');
test($a, 'hare');

?>

运行给出

$ php -f test.php                   
string 'How are you?' contains word 'are' 
string 'How are you?' does not contain word 'ar'
string 'How are you?' does not contain word 'hare'

注意:这里我们并不是指每个符号序列的单词。

单词的实际定义是 PCRE 正则表达式引擎,其中单词是仅由单词字符组成的子串,由非单词字符分隔。

“单词”字符是任何字母或数字或下划线字符,
也就是说,可以是 Perl“单词”一部分的任何字符。这
字母和数字的定义由PCRE的字符控制
表,如果发生特定于区域设置的匹配,则可能会有所不同 (..)

Check if string contains specific words?

This means the string has to be resolved into words (see note below).

One way to do this and to specify the separators is using preg_split (doc):

<?php

function contains_word($str, $word) {
  // split string into words
  // separators are substrings of at least one non-word character
  $arr = preg_split('/\W+/', $str, NULL, PREG_SPLIT_NO_EMPTY);

  // now the words can be examined each
  foreach ($arr as $value) {
    if ($value === $word) {
      return true;
    }
  }
  return false;
}

function test($str, $word) {
  if (contains_word($str, $word)) {
    echo "string '" . $str . "' contains word '" . $word . "'\n";
  } else {
    echo "string '" . $str . "' does not contain word '" . $word . "'\n" ;
  }
}

$a = 'How are you?';

test($a, 'are');
test($a, 'ar');
test($a, 'hare');

?>

A run gives

$ php -f test.php                   
string 'How are you?' contains word 'are' 
string 'How are you?' does not contain word 'ar'
string 'How are you?' does not contain word 'hare'

Note: Here we do not mean word for every sequence of symbols.

A practical definition of word is in the sense the PCRE regular expression engine, where words are substrings consisting of word characters only, being separated by non-word characters.

A "word" character is any letter or digit or the underscore character,
that is, any character which can be part of a Perl " word ". The
definition of letters and digits is controlled by PCRE's character
tables, and may vary if locale-specific matching is taking place (..)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文