Hello!

There is a piece of code

if (substr($key,0,4) == "AIR/" && in_array(substr($key,4,3),array("123","321","567","894","111","222","564","864"))) { echo "\n* $key *\n"; 

which ultimately displays the values ​​in this form:

 * AIR/564-0000906c * * AIR/321-00008cab * * AIR/222-0000806f * * AIR/111-00009067 * 

Please tell me how to count the $ key before the output. I need to get the number of how many values ​​there, the asterisk * serves as a separator, because it displays everything on one line, and I don’t know how this can be calculated.
I thought, maybe counting how many in the line AIR / will meet, such a number will come out, but it still does not work out for me. Tried to count through count ($ key), but it displays 1, because it probably counts the entire line.

Help.

I tried this:

 $n = $n + 1; 

echo "\n* $n *\n" ; displays

 * 1 * * 2 * * 3 * * 4 * 

I need to get a finite number of 6, because even if I make $ n, it will give me units.

  • honestly, i didn't understand anything)))) is it possible to rephrase the task and the problem differently? - Manitikyl

2 answers 2

I understood your task like this:

 $str = '* AIR/564-0000906c * * AIR/321-00008cab * * AIR/222-0000806f * * AIR/111-00009067 *'; $count = preg_match_all('/AIR\/[\d]{3}-[0-9a-z]{8}/', $str, $matches); echo $count; // 4 
  • Thanks for your reply. Not exactly what I'm looking for ... if code (substr ($ key, 0,4) == "AIR /" && in_array (substr ($ key, 4,3), array ("123", "321" , "567", "894", "111", "222", "564", "864"))) {// EXAMPLE if I make echo $ key here, it will output * AIR / 564-0000906c * * AIR / 321-00008cab * * AIR / 361-00008cab * I do your example, I substitute $ key for the match $ count = preg_match_all ('/ AIR \ / [\ d] {3} - [0-9a-z] {8 } / ', $ key, $ matches); echo $ count; // it gives out 111 (three digits 1), I need to get in this case 3. Do you understand what I mean? - ya_sf
  • @ya_sf, it seems to me that you are doing a search in the loop preg_match_all. If so, add up the quantities received. // somewhere at the beginning $ count = 0; // in the loop $ count + = preg_match_all ('/ AIR \ / [\ d] {3} - [0-9a-z] {8} /', $ key); - Deonis
  • Something did not help * (Roughly speaking, here is the whole section of the code if ($ air_channels) {ksort ($ air_channels); foreach ($ air_channels as $ key => $ value) {if (substr ($ key, 0.4) == "SIP /" && in_array (substr ($ key, 4,3), array ("123", "321", "658", "965", "547", "354", "346", " 348 "," 189 "," 147 "," 123 "," 157 "," 121 "," 325 "," 359 "," 796 "," 724 "," 701 "))) {// then do something - ya_sf

You can get data in a loop and check or do something with it with them.

 <?php $str = '* AIR/564-0000906c * * AIR/321-00008cab * * AIR/222-0000806f * * AIR/111-00009067 *'; preg_match_all('/AIR\/([\d]{3})-([0-9a-z]{8})/', $str, $matches, PREG_OFFSET_CAPTURE); for ($i = 0; $i < count($matches[0]); $i++) // здесь чтото делаете со своими данными echo "в ".$matches[0][$i][0]." (первое)->".$matches[1][$i][0]." (второе)->".$matches[2][$i][0]."\n"; ?> 

here is a working example