|
|
|
<?php
/* This function returns the position of string s1 within string s2.
The position is 1 based. If s1 is not in s2, 0 is returned.
*/
function InStr($s1, $s2)
{
//Check for valid input
if(!(is_string($s1) && is_string($s2))) return 0;
$s1len = strlen($s1);
$s2len = strlen($s2);
//Check if s1 in s2 at all
if(!ereg($s1, $s2)) return 0;
//Resolve simple case
if($s1 == $s2) return 1;
//Set initial search limits
$begin = 0;
$end = $s2len - $s1len;
//Initialize position
$position = 0;
//Do binary search of s2 for s1
//Check left side first to find first occurance of s1
//Check right side first to find last occurance of s1
while($end > $begin + 1)
{
$middle = ceil(($begin + $end) / 2);
$leftBegin = $begin;
$rightBegin = $middle + $s1len;
$leftEnd = $middle;
$rightEnd = $end + $s1len;
//Check left first
if(ereg($s1, substr($s2, $leftBegin, $rightBegin - $leftBegin)))
{
$end = $middle;
}
else //(ereg($s1, substr($s2, $leftEnd, $rightEnd - $leftEnd)))
{
$position += $middle - $begin;
$begin = $middle;
}
}
//Resolve 1 off problems introduced by ceil
if(ereg($s1, substr($s2, $end, $s1len))) $position++;
//Return position 1 based
return $position + 1;
}
?> |
|
| Check parameters validity. Paranoia was designed to check the validity of the parameters that a php page will receive after a form submission. It can be used to check the variables sent by POST or GET Categories : Algorithms, HTML and PHP, PHP, Variables | | | A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL | | | Boolean Keyword Interpreter Categories : PHP, Algorithms, Search Engines | | | Diffusion-Limited Aggregation visualization Categories : PHP, Graphics, Algorithms, Math. | | | Browse a MySQL database & draw a tree view & load final items into a template page. Categories : MySQL, Complete Programs, Algorithms, PHP, Databases | | | Recursive function to move files on a filesystem. It can be minor changed in order to copy recursively.
Categories : PHP, Filesystem, Algorithms | | | Fast PI calculator. Can easily find the 1000th decimal place of pi in 5 seconds. Categories : PHP, BC math, Algorithms | | | Mail-lib provides a simple interface to the sendmail program. Note: you must actually have sendmail on your machine (sorry windows NT users). Categories : Algorithms, Email, PHP | | | A recursive function to traverse a multi-dimensional array where the
dimensions are not known Categories : Arrays, PHP, Algorithms | | | The Porter Word Stemming Algorithm in PHP
Reduces words to their base stem for search engines and indexing Categories : Algorithms, PHP, Strings | | | SHA: Implementation of the Secure Hash Algorithm in pure PHP. This is a secure one-way function that can be used to perform challenge
response login algorithms over an insecure connection. Categories : Algorithms, PHP, Security | | | minus - subtract arrays. Send two arrays and get an array with the operation A-B, elements on A that are not included on B. Categories : PHP, Arrays, Algorithms | | | Calculate Body Mass Index Categories : PHP, Algorithms, Regexps | | | A simple bubblesort that takes 2 arrays as argument.The first one is the actual data used for sorting, the second is data that will "tag along" with the first array, for instance a descriptive text about the data in the first array. Categories : Algorithms, Arrays, PHP, Complete Programs | | | Paginating the mySQL data Categories : PHP, Algorithms, Databases, MySQL, HTML and PHP | |
|
|
|