00001 <?php 00002 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00003 * This file is part of Travelsized CMS 00004 * A content management system with modules, based on wiki syntax 00005 * 00006 * Author: Dan Jensen <admin@leinir.dk> 00007 * Copyright 2003/2004 00008 * 00009 * This program is free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * The GNU General Public License is available at: http://www.gnu.org/copyleft/ 00015 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00016 00028 function strposnth($haystack, $needle, $nth=1, $insenstive=0) { 00029 //if its case insenstive, convert strings into lower case 00030 if ($insenstive) { 00031 $haystack=strtolower($haystack); 00032 $needle=strtolower($needle); 00033 } 00034 //count number of occurances 00035 $count=substr_count($haystack,$needle); 00036 00037 //first check if the needle exists in the haystack, return false if it does not 00038 //also check if asked nth is within the count, return false if it doesnt 00039 if ($count<1 || $nth > $count) return false; 00040 00041 //run a loop to nth number of accurance 00042 for($i=0,$pos=0,$len=0;$i<$nth;$i++) { 00043 //get the position of needle in haystack 00044 //provide starting point 0 for first time ($pos=0, $len=0) 00045 //provide starting point as position + length of needle for next time 00046 $pos=strpos($haystack,$needle,$pos+$len); 00047 00048 //check the length of needle to specify in strpos 00049 //do this only first time 00050 if ($i==0) $len=strlen($needle); 00051 } 00052 00053 //return the number 00054 return $pos; 00055 } 00056 00057 function strleft($s1, $s2) { 00058 return substr($s1, 0, strpos($s1, $s2)); 00059 } 00060 00061 /* 00062 PHP function 00063 * What this is: 00064 count_words() counts the number of words in a string and returns a word count, cleaning out occurances of continous spaces, taking all latin characters into consideration and also numbers. 00065 So: "Frites - à la année 1984 ..." is 5 words... 00066 Distributed by Nick Brandt (nicknbrandt.com) 00067 Last modified 9/Jul/2003 21:59 00068 * Usage: 00069 $word_count = count_words($text); 00070 * License 00071 This program is free software; you can redistribute it and/or modify 00072 it under the terms of the GNU General Public License as published by 00073 the Free Software Foundation; either version 2 of the License, or 00074 (at your option) any later version. 00075 00076 This program is distributed in the hope that it will be useful, 00077 but WITHOUT ANY WARRANTY; without even the implied warranty of 00078 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00079 GNU Library General Public License for more details. 00080 You should have received a copy of the GNU Library General Public License 00081 along with this program; if not, write to the Free Software 00082 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00083 Please contact me (nick@nbrandt.com) if you use this script or if you have any 00084 comments or additions. Also feel free to contact me if you need a programmer 00085 (*UNIX, Apache, MySQL, PHP....). 00086 My C.V. is available on http://www.nbrandt.com 00087 */ 00088 // To test, uncomment the 3 lines below and call the script. 00089 // $text = "Frites - à la année 1984 ..."; 00090 // $word_count = count_words($text); 00091 // echo " - $word_count - "; 00092 // The code you need is below: 00093 function count_words($string) { 00094 $word_count = 0; 00095 $string = eregi_replace(" +", " ", $string); 00096 $string = explode(" ", $string); 00097 while (list(, $word) = each ($string)) { 00098 if (eregi("[0-9A-Za-zÀ-ÖØ-öø-ÿ]", $word)) { 00099 $word_count++; 00100 } 00101 } 00102 return($word_count); 00103 } 00104 ?>