PHP - 如何检查字符串是否包含特定文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15305278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:01:20  来源:igfitidea点击:

PHP - How to check if a string contains a specific text

php

提问by Chead

<?php
$a = '';

if($a exist 'some text')
    echo 'text';
?>

Suppose I have the code above, how to write the statement "if($a exist 'some text')"?

假设我有上面的代码,如何写语句“if($a exists 'some text')”?

回答by Dai

Use the strposfunction: http://php.net/manual/en/function.strpos.php

使用strpos函数:http: //php.net/manual/en/function.strpos.php

$haystack = "foo bar baz";
$needle   = "bar";

if( strpos( $haystack, $needle ) !== false) {
    echo "\"bar\" exists in the haystack variable";
}

In your case:

在你的情况下:

if( strpos( $a, 'some text' ) !== false ) echo 'text';

Note that my use of the !==operator (instead of != falseor == trueor even just if( strpos( ... ) ) {) is because of the "truthy"/"falsy"nature of PHP's handling of the return value of strpos.

请注意,我使用的!==运营商(而不是!= false或者== true甚至仅仅if( strpos( ... ) ) {)是因为的“truthy” /“falsy”的PHP处理的返回值的性质strpos

回答by Blender

Empty strings are falsey, so you can just write:

空字符串是假的,所以你可以写:

if ($a) {
    echo 'text';
}

Although if you're asking if a particular substring exists in that string, you can use strpos()to do that:

尽管如果您询问该字符串中是否存在特定的子字符串,您可以使用以下strpos()方法:

if (strpos($a, 'some text') !== false) {
    echo 'text';
}

回答by Leeish

http://php.net/manual/en/function.strpos.phpI think you are wondiner if 'some text' exists in the string right?

http://php.net/manual/en/function.strpos.php我想如果字符串中存在“某些文本”,您会感到奇怪吗?

if(strpos( $a , 'some text' ) !== false)

回答by Havenard

You can use strpos()or stripos()to check if the string contain the given needle. It will return the position where it was found, otherwise will return FALSE.

您可以使用strpos()stripos()检查字符串是否包含给定的针。它将返回找到它的位置,否则将返回 FALSE。

Use the operators ===or `!== to differ FALSE from 0 in PHP.

===在 PHP 中使用运算符或 `!== 来区分 FALSE 和 0。

回答by Jesse

If you need to know if a word exists in a string you can use this. As it is not clear from your question if you just want to know if the variable is a string or not. Where 'word' is the word you are searching in the string.

如果您需要知道字符串中是否存在某个单词,您可以使用它。因为从您的问题中不清楚您是否只想知道变量是否是字符串。其中“单词”是您在字符串中搜索的单词。

if (strpos($a,'word') !== false) {
echo 'true';
}

or use the is_string method. Whichs returns true or false on the given variable.

或使用 is_string 方法。对于给定的变量,whichs 返回 true 或 false。

<?php
$a = '';
is_string($a);
?>

回答by Kermit

You can use the ==comparison operatorto check if the variable is equal to the text:

您可以使用==比较运算符来检查变量是否等于文本:

if( $a == 'some text') {
    ...

You can also use strposfunction to return the first occurrence of a string:

您还可以使用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";
}

See documentation

查看文档

回答by P44T

Do mean to check if $a is a non-empty string? So that it contains just any text? Then the following will work.

是否意味着检查 $a 是否为非空字符串?所以它只包含任何文本?然后以下将起作用。

If $a contains a string, you can use the following:

如果 $a 包含字符串,则可以使用以下内容:

if (!empty($a)) {      // Means: if not empty
    ...
}

If you also need to confirm that $a is actually a string, use:

如果您还需要确认 $a 实际上是一个字符串,请使用:

if (is_string($a) && !empty($a)) {      // Means: if $a is a string and not empty
    ...
}

回答by Maneesh Mehta

you can use this code

你可以使用这个代码

$a = '';

if(!empty($a))
  echo 'text';