Subversion Repositories travelsized

Rev

Rev 73 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * This file is part of Travelsized CMS
 *              A content management system with modules, based on wiki syntax
 *
 * Author: Dan Jensen <admin@leinir.dk>
 * Copyright 2003/2004
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The GNU General Public License is available at: http://www.gnu.org/copyleft/
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


/**
 * Str Pos Nth (Position of nth occurance of a string)
 * A handy function to get the position of nth occurance of a substring in a string, with an optional param to make it case insenstive. I am calling it strposnth, suggestions welcome.
 *
 * Third optional parameter gets the value of n, e.g puting in 2 will return position of second occurance of needle in haystack: Valid inputs (1 = default) 2,3,4.....
 *
 * Fourth optional parameter can be used to specify the function as case insenstive: Valid inputs (0 = case senstive = default) 1 = case insenstive.
 *
 * Courtesy of webKami [at] akdomains.com in the php.net manual
 */


function strposnth($haystack, $needle, $nth=1, $insenstive=0) {
        //if its case insenstive, convert strings into lower case
        if ($insenstive) {
                $haystack=strtolower($haystack);
                $needle=strtolower($needle);
        }
        //count number of occurances
        $count=substr_count($haystack,$needle);
       
        //first check if the needle exists in the haystack, return false if it does not
        //also check if asked nth is within the count, return false if it doesnt
        if ($count<1 || $nth > $count) return false;
       
        //run a loop to nth number of accurance
        for($i=0,$pos=0,$len=0;$i<$nth;$i++) {
                //get the position of needle in haystack
                //provide starting point 0 for first time ($pos=0, $len=0)
                //provide starting point as position + length of needle for next time
                $pos=strpos($haystack,$needle,$pos+$len);

                //check the length of needle to specify in strpos
                //do this only first time
                if ($i==0) $len=strlen($needle);
        }

        //return the number
        return $pos;
}

function strleft($s1, $s2) {
        return substr($s1, 0, strpos($s1, $s2));
}

/*
PHP function
* What this is:
 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.
So: "Frites - à la année 1984 ..." is 5 words...
Distributed by Nick Brandt (nicknbrandt.com)
 Last modified 9/Jul/2003 21:59
* Usage:
     $word_count = count_words($text);
* License
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Please contact me (nick@nbrandt.com) if you use this script or if you have any
 comments or additions. Also feel free to contact me if you need a programmer
 (*UNIX, Apache, MySQL, PHP....).
My C.V. is available on http://www.nbrandt.com
 */

// To test, uncomment the 3 lines below and call the script.
 // $text = "Frites - à la année 1984 ...";
 // $word_count = count_words($text);
 // echo " - $word_count - ";
// The code you need is below:
 function count_words($string) {
     $word_count = 0;
    $string = eregi_replace(" +", " ", $string);
     $string = explode(" ", $string);
     while (list(, $word) = each ($string)) {
         if (eregi("[0-9A-Za-zÀ-ÖØ-öø-ÿ]", $word)) {
             $word_count++;
         }
     }
     return($word_count);
 }
?>