Login   Register  
PHP Classes
elePHPant
Icontem

File: biblio_string

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Julien PACHET  >  Form Classic  >  biblio_string  >  Download  
File: biblio_string
Role: Auxiliary script
Content type: text/plain
Description: Functions to manipulaite strings
Class: Form Classic
Generate the HTML for forms with common inputs
Author: By
Last change:
Date: 2004-03-22 16:08
Size: 7,377 bytes
 

Contents

Class file image Download
<?php

////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FILE NAME :  Biblio_string.inc.php                                                                     //
// LANGUAGE  :  PHP                                                                                       //
// AUTHOR    :  Julien PACHET                                                                             //
// EMAIL     :  j|u|l|i|e|n| [@] |p|a|c|h|e|t.c|o|m                                                       //
// VERSION   :  1.0                                                                                       //
// CREATION  :  05/11/2003                                                                                //
// LICENCE   :  GNU/GPL                                                                                   //
////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////
// What the library does:                                                                                 //
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// * Help manipulate strings                                                                               //
////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////// 
// Changelog:                                                                                             // 
// ----------                                                                                             // 
//  Date        Version   Actions                                                                         // 
// ------------------------------------------------------------------------------------------------------ // 
//  05/11/2003  1.0       Tested & Final version                                                          //
//  29/02/2004  1.1       new function string_nearest                                                     //
//  19/02/2004  1.15      function string_nearest now return the entire tab, sorted by nearest value      //
////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Need to work:                                                                                          //
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// * other file:                                                                                          //
//   * biblio_array.inc.php: help managing arrays                                                         //
////////////////////////////////////////////////////////////////////////////////////////////////////////////
require_once("biblio_array.inc.php");

////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Declaration                                                                                            //
// -----------                                                                                            //
// string_alternative($n,$if_0,$if_1,$if_n)                                                               //
// string_piece($str,$a,$b)                                                                               //
// string_add_right($str,$padding,$total_length)                                                          //
// string_add_left($str,$padding,$total_length)                                                           //
// string_part($separator,$str,$index)                                                                    //
// string_surround($text,$pre,$post)                                                                      //
// string_nearest($tab,$search)                                                                                                                  //
////////////////////////////////////////////////////////////////////////////////////////////////////////////

function string_alternative($n,$if_0,$if_1,$if_n) {
    
/**
    * string_alternative()    : return $if_0, $if_1 or $if_n from the value of $n
    * 
    * @param $n
    * @param $if_0                    : string to return if $n=0
    * @param $if_1                    : string to return if $n=1
    * @param $if_n                    : string to return if $n>1
    * @return
    */
    
if ($n==0)    return $if_0;
    if (
$n==1)    return $if_1;
    if (
$n>1)        return $if_n;
    }
    
function 
string_piece($str,$a,$b) {
    
/**
    * string_piece()        : cut the string for a number to b number of character
    * 
    * @param $str            : string to process
    * @param $a                : start character
    * @param $b                : final character
    * @return
    */
    
return substr($str,$a,$b-$a+1);
}

function 
string_add_right($str,$padding,$total_length) {
    
/**
    * string_add_left()            : cut $str if it's too long and add $padding character after to get the total length
    * 
    * @param $str                         : string to process
    * @param $padding                 : the character(s)to use
    * @param $total_length     : the final size to have
    * @return
    */
    
$temp="";
    if (
strlen($str)>$total_length)
        
$temp=copyab($str,0,$total_length-1);
    return 
$temp.str_repeat($padding,$total_length-strlen($temp));
}

function 
string_add_left($str,$padding,$total_length) {
    
/**
    * string_add_left()            : cut $str if it's too long and add $padding character before to get the total length
    * 
    * @param $str                         : string to process
    * @param $padding                 : the character(s)to use
    * @param $total_length     : the final size to have
    * @return 
    */
    
$nb=strlen($str);
    
$temp=($nb>$total_length)?copyab($str,0,$total_length-1):"";
    return  
str_repeat($padding,$total_length $nb).$str;
}

function 
string_part($separator,$str,$index) {
    
/**
 * string_part()            : return the # part separated by $separator
 * 
 * @param $separator    : the separator
 * @param $str                : the string to cut
 * @param $index            : the number of part to return
 * @return
 */
    
$temp=explode($separator,$str);
    if ((
$index>=0)&&($index<count($temp)))
        return 
$temp[$index];
    else
        return 
false;
}

function 
string_surround($text,$pre,$post) {
    
/**
 * string_surround()    : surround the text by $pre et $post
 * 
 * @param $text                : $text to surround
 * @param $pre                : text to put before
 * @param $post                : text to put after
 * @return 
 */
    
return $pre.$text.$post;
}

function 
string_nearest($tab,$search) {
    
/**
 * string_nearest()    : find index of nearest item in array $tab using levenshtein function with
 *                      value 1 for insert,
 *                      value 2 for replace,
 *                      value 4 for delete
 * 
 * @param $tab        : $array of value
 * @param $search     : item to search 
 * @return 
 */
    
    
foreach ($tab as $k=>$v)
        
$temp1[]=array("value"=>$v,"score"=>levenshtein($v,$search,1,2,4));
    
$res=array_2D_transpose(array_2D_sort($temp1,"score","ASC"));
    return 
$res["value"];
}

?>