We have a string consisting of several numbers separated by commas:
$allNam = '12,2,41,51,8,53';
It is necessary to find the number 51 in the $ allNam variable and if it is there to output True.
We have a string consisting of several numbers separated by commas:
$allNam = '12,2,41,51,8,53';
It is necessary to find the number 51 in the $ allNam variable and if it is there to output True.
1 option
function search($str, $search) { return false !== stripos($str, $search); } $allNam = '12,2,41,51,8,53'; $result = search($allNam, 51);
Option 2
$allNam = '12,2,41,51,8,53'; $result = in_array(51, explode(',', $allNam));
You could add a ΡΠ΅Π³ΡΠ»ΡΡΠ½ΡΠ΅ Π²ΡΡΠ°ΠΆΠ΅Π½ΠΈΡ
label to this question ΡΠ΅Π³ΡΠ»ΡΡΠ½ΡΠ΅ Π²ΡΡΠ°ΠΆΠ΅Π½ΠΈΡ
(^|[^\d]+)51([^\d]+|$)
https://regex101.com/r/QvkJhX/1
(^|[^\d]+) Π² Π½Π°ΡΠ°Π»Π΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΈΠ»ΠΈ Π½Π°ΡΠ°Π»ΠΎ ΡΡΡΠΎΠΊΠΈ ΠΈΠ»ΠΈ Π½Π΅ ΡΠΈΡΡΡ 51 ΠΈΡΠΊΠΎΠΌΠΎΠ΅ ΡΠΈΡΠ»ΠΎ ([^\d]+|$) ΡΠ°ΠΊ ΠΆΠ΅ ΠΊΠ°ΠΊ ΠΈ Π² Π½Π°ΡΠ°Π»Π΅ ΡΠΎΠ»ΡΠΊΠΎ ΠΊΠΎΠ½Π΅Ρ ΡΡΡΠΎΠΊΠΈ
The function itself http://php.net/manual/ru/function.preg-match.php
function testNumber( $n, $subject ) { $reg = '/(^|[^\d]+)'. $n .'([^\d]+|$)/'; return preg_match ( $reg , $subject ); } var_dump(testNumber( 53, '12,2,41,51,8,53' )); // int 1 var_dump(testNumber( 10, '12,2,41,51,8,53' )); // int 0 var_dump(testNumber( 12, '12,2,41,51,8,53' )); // int 1
Source: https://ru.stackoverflow.com/questions/960721/
All Articles