zero-perfoliate
zero-perfoliate

Author Topic: Array search string problem  (Read 233 times)

Offline Nakayama

  • New PHP Members
  • Posts: 1
  • Karma: +0/-0
Array search string problem
« on: May 20, 2010, 07:44:08 AM »
hi there.

i've got a problem.

imagine the following array containing several elements (lines) similar to this: tcp 0 0 0.0.0.0:12345 0.0.0.0:* LISTEN 78965 /sshd etc etc

i've been using this code to search by "12345". i want to count how many times it apears in the array.

$var = "12345";

if(!in_array( $var, $users )) {
echo "not found.";
} else {
$cont = $cont + 1;

}

echo $cont;


problem: but this code searches complete lines, not strings in the lines... only findes if i put $var = "tcp 0 0 0.0.0.0:12345 0.0.0.0:* LISTEN 78965 /sshd etc etc" ...


Thanks in advance!  :)

Offline Sergey Popov

  • PHP Helpers
  • ***
  • Posts: 31
  • Karma: +0/-0
    • Freelance PHP Developer
Re: Array search string problem
« Reply #1 on: May 25, 2010, 09:38:52 AM »
To search by partial string you can use strpos() or preg_match_all().
Here is the code:


<?php

  $var 
"12345";
  
$cont 0;

  foreach(
$users as $line){
    if(
strpos($line,$var)!==false$cont++;
  }

  echo 
$cont;

?>
Refer to php documentation or PHP Help forum for more info and for answers to your questions

 

zero-perfoliate