Home  >  Article  >  Backend Development  >  PHP simple method to determine whether a string contains another string_php tips

PHP simple method to determine whether a string contains another string_php tips

WBOY
WBOYOriginal
2016-05-16 19:55:381507browse

The example in this article describes how PHP simply determines whether a string contains another string. Share it with everyone for your reference, the details are as follows:

When using PHP to determine whether a string contains a simple substring, we usually use the two functions strpos() or stristr(), but if the position of the string is in At index 0, that is to say at the beginning of the string to be matched, there will be a problem.

Of course we still have a solution. Here is a stupid solution, but it is easier to use. It applies to both Chinese and English punctuation marks.

The code is as follows:

function checkStr($str,$target)
{
  $tmpArr = explode($str,$target);
  //print_r($tmpArr);
  if(count($tmpArr)>1)return true;
  else return false;
}
$result = checkStr("sdfg","bsdfsldkfj");
var_dump($result);

Readers who are interested in more PHP-related content can check out the special topics on this site: "Summary of PHP operating office document skills (including word, excel, access, ppt)", "php date Time usage summary", "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation Introductory tutorial " and " Summary of common PHP database operation skills "

I hope this article will be helpful to everyone in PHP programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn