This function helps you with manipulating strings in array elements, even nested functions if you want. This function will only work with single arrays, so no multidimensional array support!
Check the examples so you would get an idea.
Simple explanation: Run an certain string manipulation function over each of the array elements.
<?php
/**
* StringFunc()
* Manipulate strings with string functions
* @param mixed $arr
* @param mixed $action
* @return array $New
*/
function StringFunc( $arr, $action )
{
// Create new array for results
$New = array();
// foreach array element
foreach ( $arr as $key => $value )
{
// if is action var is an array
if ( is_array( $action ) )
{
// foreach of the actions
foreach ( $action as $func )
{
// Manipulate the values
$value = call_user_func( $func, $value );
}
// Put in array
$New[$key] = $value;
} else
{
// Single action, perform immediately
$New[$key] = call_user_func( $action, $value );
}
}
// Return new filled array
return $New;
}
?>
Example 1
If you want to use nested functions, place them into the array in order as you would do it normally do nested functions.
The first array element would be the most inner function, and the last array element would be the most outer one.
In this case, the function strtolower() would be performed before ucfirst() will be performed.
// Return the new array $new = StringFunc( array( 'CAPITAL CHARACTERS', 'DiFfErEnT ChArAcTeRs' ), array( 'strtolower', 'ucfirst' ) );
Result 1
Array
(
[0] => Capital characters
[1] => Different characters
)
Example 2
// Return the new array $new = StringFunc( array( 'CAPITAL CHARACTERS', 'DiFfErEnT ChArAcTeRs' ), 'strtolower' );
Result 2
Array
(
[0] => capital characters
[1] => different characters
)
Mail this!
- Comments (0)
- PingBacks (0)
- TrackBacks (0)


» Latest comments