You are here: Re: [PHP] counting ocuring words in a string « PHP « IT news, forums, messages
Re: [PHP] counting ocuring words in a string

Posted by Brian V Bonini on 05/17/05 16:55

On Tue, 2005-05-17 at 06: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.

Maybe something like this?

<?php

$words = array("php", "language");
$str = 'One language which is great is php. PHP works great!';
$str = strtolower($str);

echo '<pre>';

foreach($words as $word) {
echo 'word = ' .$word;
echo ' (' . substr_count($str, $word). ")\n";
}

echo '</pre>';

?>

Although you'll run into a problem if your words array contains for
example 'air' which will match both 'air' and 'fair',

--

s/:-[(/]/:-)/g


Brian GnuPG -> KeyID: 0x04A4F0DC | Key Server: pgp.mit.edu
======================================================================
gpg --keyserver pgp.mit.edu --recv-keys 04A4F0DC
Key Info: http://gfx-design.com/keys
Linux Registered User #339825 at http://counter.li.org

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация