1

假设我有:

$strPar = "This is a simple paragraph that we will use for the questioning";
$strFindMe = "that";

我将如何检查是否$strPar包含$strFindMe

4

6 回答 6

2

最快的方法是使用strpos

  $exists = strpos($strPar, $strFindMe);
  if ($exists !== false) {
    // substring is in the main string
  }
于 2013-05-31T07:02:11.403 回答
1

尝试这样的事情

if (false !== strpos($strPar, $strFindMe ) )
于 2013-05-31T07:02:30.290 回答
1
$string = "This is a strpos() test";
$pos = strpos($string, "i", 3);

    if ($pos === false) {
     print "Not found\n";
}else{
     print "Found at $pos!\n";
}
于 2013-05-31T08:30:17.947 回答
0
<?php
$strPar = "This is a simple paragraph that we will use for the questioning";
$strFindMe   = "that";
$pos = strpos($strPar, $strFindMe);

// 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";
}
?>
于 2013-05-31T07:04:50.217 回答
0

使用strpos()函数检查,它区分大小写!

if( strpos($strPar, $strFindMe) ) {  //it return a boolean value
   echo "String Found";
}
于 2013-05-31T07:14:50.583 回答
0
$strPar = "This is a simple paragraph that we will use for the questioning";
$strFindMe = "THAT";//Find the position of the first occurrence of a case-insensitive substring in a string
$exists = strpos($strPar, $strFindMe);
  if ($exists !== false) {
    // substring is in the main string
  }
于 2013-05-31T10:25:51.770 回答