Topic: how to filter something out of an array

Hi,

Lets say that I have the next array:

Array
(
    [0] => user_id_2-fbcss.png
    [1] => userid100_ spongebob - kopie (2).jpeg
    [2] => userid2_kierownik stamplaatje definitief 1 - kopie - kopie.jpg
    [3] => userid2_spongebob&&&.jpeg
    [4] => userid2_spongebob&&&kopie.jpeg
    [5] => userid2_test.jpg
    [6] => userid3_kierownik stamplaatje definitief 1.jpg
    [7] => userid5_kierownik stamplaatje definitief 1 - kopie (2).jpg
    [8] => userid5_spongebob - kopie - kopie.jpeg
)

Now I want to count the number of files containing 'userid2'.

I have tried preg_match but I do not understand those expressions and also tried str_replace but till now no luck sad

Can anyone tell me how I can do that.

Re: how to filter something out of an array

foreach, preg_match, counter?

http://fluxbb.org

Free PunBB Hosting - lots of mods, easy to customize

3 (edited by kierownik 2008-03-10 18:46)

Re: how to filter something out of an array

I thought so to but

$i = 0;
  foreach ( $files as $key => $file ) {
    if ( preg_match( $user_id, $file ) ) {
      $i++;
    }
  }
  echo '<p>'.$i.'</p>';

does not give anything.

Re: how to filter something out of an array

Is $user_id a valid regular expression? And if it's just a number or a plain string or something, use strpos.

http://fluxbb.org

Free PunBB Hosting - lots of mods, easy to customize

5 (edited by kierownik 2008-03-10 19:03)

Re: how to filter something out of an array

hmm, am I that stupid hmm

Array
(
    [0] => user_id_2_fbconfig.png
    [1] => userid_20_userid_2_ - kopie.png
    [2] => userid_2_fbconfig.png
)

$user_id = '/^userid_'.$pun_user['id'].'_(.*?)/';

$i = 0;
  foreach ( $files as $key => $file ) {
    if ( preg_match( $user_id, $file ) ) {
      $i++;
    }
  }
  echo '<p>'.$i.'</p>';

Now it shows just 1 if $user_id = 2

I do not know why it did not work the first time hmm
Can I now also safely say that only the first userid_2_ is counted, this because when someone uploads a file that is called userid_50_userid_2_.png
that not the second userid_2_ is getting counted?
the first userid is added by the script to determine who uploaded the file.

6 (edited by MattF 2008-03-12 00:57)

Re: how to filter something out of an array

The ^ means it's only starting the match from the beginning of the line. Remove the ^ and it will match that expression anywhere within the line. For pcre/regex, the best source of documentation is the pcrepattern section of the manpage over on pcre.org:

http://pcre.org/pcre.txt

Re: how to filter something out of an array

$user_id = '/userid_?'.$pun_user['id'].'_(.*?)/';

Re: how to filter something out of an array

aha, if ^ means that it has to start with it then it is enough, thanks.

9

Re: how to filter something out of an array

elbekko wrote:
$user_id = '/userid_?'.$pun_user['id'].'_(.*?)/';

That first question mark is a bit out of place, is it not? One assumes he would implicitly expect the underscore to be there.

Re: how to filter something out of an array

Well, according to his examples it was both possible with and without the underscore. Or atleast, that's how I saw it.

Re: how to filter something out of an array

yes that was my mistake, I did not copy that wright roll
It should be all userid_$id_ smile

but it works now smile

12

Re: how to filter something out of an array

elbekko wrote:

Well, according to his examples it was both possible with and without the underscore. Or atleast, that's how I saw it.

My apologies. I hadn't noted the first example. big_smile