Home  >  Article  >  Backend Development  >  Determine whether a string of multiple numbers contains a unique number

Determine whether a string of multiple numbers contains a unique number

WBOY
WBOYOriginal
2016-08-08 09:07:051502browse

$str=10,11,12,13,14,15,16,17,18,19;
$str2=9;

I want to determine whether this $str2 is in $str, but I can still find it using PHP’s own function strpos because 19 also contains 9. The effect I want is to judge whether each string is equal to this $str2 by commas. Please help me. Thank you. Endless^_^

Reply content:

$str=10,11,12,13,14,15,16,17,18,19;
$str2=9;

I want to determine whether this $str2 is in $str, but I can still find it using PHP’s own function strpos because 19 also contains 9. The effect I want is to judge whether each string is equal to this $str2 by commas. Please help me. Thank you. Endless^_^

First separate the elements into an array by commas and use the explode() function. Then use the in_array() function to find out whether there is an element 9.

Remove the separator (comma) to only one element, and then use stripos. Note that the possible position is 0, so use === false

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