I would like to share with you a couple of functions that use a lot and I consider them quite useful.
I also recommend PHP hosting for all PHP based websites and apps. It will ensure smooth work of the scripts.
Function is used to insert a string within another string. The first argument is the base. The second string is inserted in base and base is forwarded by a reference. The second argument is a string that is inserted into the base. The third argument is a number that tells from which character do we start inserting. If the number is greater than the total length of the base, string will be appended. If the number is less than 0, string is inserted at the beginning of the base.
function insert ( &$base, $string, $begin ) {
$base = ( string ) $base;
$string = ( string ) $string;
$begin = ( int ) $begin;
if ( strlen($base) < $begin ) {
$base .= $string;
}
elseif ( $begin < 0 ) {
$base = $begin . $base;
}
else {
$base = substr ( $base, 0, $begin ) . $string . substr ( $base, $begin );
}
}
Example:
$baza = 'Danas je dan.'; //String as base $string = 'lijep suncan '; //String that we insert insert ( $baza, $string, 9 ); //Function call (we want insert $string after 9. character in $baza) echo $baza; //Result is "Danas je lijep suncan dan."
Function is used to highlight second string in first string. The first argument is the base, which is forwarded as a reference. The second argument is a string that is highlighted in base. The third argument is the string that is at the beginning of string we are looking for. Third argument the string that is at the end of string that we are looking for. The fourth argument determines whether we use a regular expression or not. The fifth argument determines whether we use case sensitive search (no impact if you use regular expressions).
function highlight ( &$base, $string, $wrapStart, $wrapEnd, $regex = false, $case = true ) {
$base = ( string ) $base;
$string = ( string ) $string;
$wrapStart = ( string ) $wrapStart;
$wrapEnd = ( string ) $wrapEnd;
$regex = ( bool ) $regex;
$case = ( bool ) $case;
if ( $regex ) {
preg_replace ( $string, $wrapStart . "$1" . $wrapEnd, $base );
}
else {
$func = $case ? 'strpos' : 'stripos';
$pos = 0;
$wrapStartLen = strlen ( $wrapStart );
$wrapEndLen = strlen ( $wrapStart );
$stringLen = strlen ( $string );
while ( ( $pos = call_user_func ( $func, $base, $string, $pos ) ) !== false ) {
$base = substr ( $base, 0, $pos ) . $wrapStart . substr ( $base, $pos, strlen ( $string ) ) . $wrapEnd . substr ( $base, $stringLen + $pos );
$pos += $wrapStartLen + $wrapEndLen + 1;
}
}
}
Example:
$baza = 'Danas je lijep suncan dan.'; //String in which we highlight $sting = 'Dan'; //String that we highlight $wrapStart = '<strong>'; //Start tag $wrapEnd = '</strong>'; //End tag highlight ( $baza, $sting, $wrapStart, $wrapEnd, false, false ); //We call function (we do not want regular expression and we want it to be case insensitive) echo $baza; //Result is "<strong>Dan</strong>as je lijep suncan <strong>dan</strong>."
Function has a simple purpose. She will turn multidimensional array into one-dimensional array. It's a recursive function and she receives only one argument. This argument is array that we want to flatten. Output value of function is one, one-dimensional array. It is useful to use the ksort in order to sort array because returned array is not sorted.
function makeFlat ( $array ) {
$array = ( array ) $array;
$keys = array_keys ( $array );
do {
$key = current ( $keys );
if ( is_array ( $array [$key] ) ) {
arrayMerge ( false, $array, makeFlat ( $array [$key] ) );
unset ( $array [$key] );
}
} while ( next ( $keys ) !== false );
return $array;
}
Example:
$array = array( 0, array( 1, 2, array( array( 3, 4, 5 ), 6, 7, 8 ), 9, 10, array( 11, 12, 13 ) ), 14, 15 ); //We create multidimensional array $niz = makeFlat ( $array ); //Function call print_r ( $niz ); /* Result is "Array ( [0] => 0 [2] => 14 [3] => 15 [4] => 1 [5] => 2 [6] => 9 [7] => 10 [8] => 6 [9] => 7 [10] => 8 [11] => 3 [12] => 4 [13] => 5 [14] => 11 [15] => 12 [16] => 13 )" */
Combines all arrays into one. The first argument determines how we treat keys that have a duplicate. If true, the function replaces the previous value with the new. If false, the function creates a new key for value. The second argument is array that is used as a base and is forwarded by reference. Further you can add as many arrays as you want and they will be merged into first array. This function is required for the proper work of makeFlat function.
function arrayMerge ( $replace = false, &$baseArray ) {
$replace = ( bool ) $replace;
$args = func_num_args ();
if ( $args < 3 ) {
return true;
}
for ( $i = 2; $i < $args; $i++ ) {
$array = ( array ) func_get_arg ( $i );
$keys = array_keys ( $array );
do {
$key = current ( $keys );
if ( isset ( $baseArray [$key] ) ) {
if ( $replace )
$baseArray [$key] = $array [$key];
else
$baseArray [] = $array [$key];
}
else {
$baseArray [$key] = $array [$key];
}
} while ( next ( $keys ) !== false );
}
return true;
}
Example:
$array1 = array(
0,
1,
2,
3
);
$array2 = array(
4,
5,
6
);
$array3 = array(
7,
8,
9,
10
);//We create 3 arrays
arrayMerge ( false, $array1, $array2, $array3 ); //We call function (merge 3 arrays and create new key for duplicated keys)
print_r ( $array1 );
/*
Result is
"Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
[7] => 7
[8] => 8
[9] => 9
[10] => 10
)"
*/
The function returns the total number of rows in a multidimensional array, and it is recursive. The first argument is array in which rows are counted. Output value is the total number of rows. Example: Those would be some of the functions that I consider useful. If you have some that you find useful, put them in a comment. You will make many people happy.
function countWholeArray ( $array ) {
$array = ( array ) $array;
$keys = array_keys ( $array );
$count = count ( $array );
do {
$key = current ( $keys );
if ( is_array ( $array [$key] ) ) {
$count += countWholeArray ( $array [$key] );
}
} while ( next ( $keys ) !== false );
return $count;
}
Example:
$array = array( 0, array( 1, 2, array( array( 3, 4, 5 ), 6, 7, 8 ), 9, 10, array( 11, 12, 13 ) ), 14, 15 ); //Multidimensional array $var = countWholeArray ( $array ); //Function call echo $var; //Result is "20"
Those would be some of the functions that I consider useful. If you have some that you find useful, put them in a comment. You will make many people happy.
I make a site with php script for search.
How can I make highlight only the term searched in all place rezulted?