Re: counting ocuring words in a string

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tuesday 17 May 2005 13:00, Merlin wrote:
> Hi there,
>
> I am trying to find a way to count the number of times (if any) words are
> inside a string. So I played around with ereg, preg_match_all and so on,
> but could not put together a working code.
>
> Can anybody help me on that?
>
> This is the current code:
> function count_words($str, $words) {
>    if(is_array($words)) {
>     foreach($words as $k => $word) {
>       $pattern[$k] = "/\b($word)\b/is";
>     }
>    }
>    else {
>     $pattern = "/\b($words)\b/is";
>    }
>    return ereg( $pattern, $str);
> }
> $words = 'php language';
> $str = 'One language which is great is php. PHP works great!';
> $num_hits = count_words($str, $word);
> echo $num_hits;
>
> I would like to get an case insensitive result. In this example: 3

http://www.php.net/manual/en/function.substr-count.php

Try this.

>
> I am a bit lost on this one. Thank you for any help.
>
> Merlin

-- 

Cyberly yours,
Petar Nedyalkov
Devoted Orbitel Fan :-)

PGP ID: 7AE45436
PGP Public Key: http://bu.orbitel.bg/pgp/bu.asc
PGP Fingerprint: 7923 8D52 B145 02E8 6F63 8BDA 2D3F 7C0B 7AE4 5436

Attachment: pgpLnMMWLGO2v.pgp
Description: PGP signature


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux