Recommend this page to a friend! |
Download |
Info | Example | Files | Install with Composer | Download | Reputation | Support forum | Blog | Links |
Ratings | Unique User Downloads | Download Rankings | ||||
Not yet rated by the users | Total: 36 | All time: 10,980 This week: 455 |
Version | License | PHP version | Categories | |||
mapache-commons 1.0 | GNU Lesser Genera... | 5 | PHP 5, Debug, Text processing, Data t... |
Description | Author | |
This package can be used to manipulate text, collections and debug PHP code. |
<?php |
It's a set of useful functions for PHP. The name is a pun (Mapache in spanish is "raccoon")
__Mapache Commons__
It's a set of useful function with the next requirements: * The function mustn't have dependencies (unless it requires a php module). * The function must be FAST and memory friendly over the syntax sugar. * The function must be able to run statically and it must be self contained. * The function must be generic and it must solve generic problems. For example, a function that calculates the VAT of a specific country is not allowed.
It's a class with a collection of functions related with arrays and lists.
> splitOpeningClosing($text,[$openingTag='('],[$closingTag=')'],[$startPosition=0],[$excludeEmpty=true],[$includeTag=false])
Split a string by an opening and closing tag and returns an array with the result.
> splitOpeningClosing('hello(a,b,c)world(d,e,f)') > returns ['hello','a,b,c','world','d,e,f']
> splitOpeningClosing{'hello{a,b,c}world{d,e,f}','{','}') > returns ['hello','a,b,c','world','d,e,f']
> splitOpeningClosing('hello(a,b(,c)world(d,e,f)') > returns ['hello','a,b(,c','world','d,e,f']
Split a string by ignoring parts of string where values are between " or '.
> splitNotString($text,$separator,[$offset=0],[$excludeEmpty=true])
Collection::splitNotString('a,b,"CC,D,E",e,f' , ",");
// returns ['a' , 'b' , 'CC,D,E' , 'e' , 'f']
It changes the case (to lower or upper case) of the keys of an array recursively
> arrayChangeKeyCaseRecursive($arr,$case=CASE_LOWER/CASE_UPPER)
$arr=['A'=>'a','b'=>'b'];
Collection::arrayChangeKeyCaseRecursive($arr);
// returns ['a'=>'a','b'=>'b']
Collection::arrayChangeKeyCaseRecursive($arr,true);
// returns ['A'=>'a','B'=>'b']
It returns the first (or all) key(s) inside an array/object in an array that matches the value of the field<br>
For example, let's say the next array
[
['name'=>'john'],
['name'=>'mary']
]
And we want to find the first "name" equals to "mary"
$array=[]; // our array with all data
$key=arraySearchField($array,'name','mary');
> arraySearchField($array,$fieldName,$value)
Collection::arraySearchField(
[['name'=>'john'],['name'=>'mary']],'name','mary');
// returns 1
Collection::arraySearchField(
[(object)['name'=>'john'],(object)['name'=>'mary']],'name','mary');
// returns 1
Collection::arraySearchField(
[['name'=>'john'],['name'=>'mary'],['name'=>'mary']],'name','mary',true);
// returns [1,2]
> isAssoc($array)
Returns true if array is an associative array, false is it's an indexed array
> first($array)
Returns the first element of an array. Sometimes the first element is not the index [0], for example ['key1'=>1,0=2] where the first element is 'key1' and not 0. This function always returns the right value.
> firstKey($array)
Returns the first key of an array.
> arrayKeyLower($arr)
Change the case of all the keys to lowercase
>arrayKeyUpper($arr)
Change the case of all the keys to lowercase
>generateTable($array,$css=true)
Generate a html table from an array
It's a class with a collection of functions related with debug.
>var_dump($value,$console=false)
Alternative to var_dump. It "pre" the result or it shows the result in the console of javascript.
>var_dump($value,true) // returns a var_dump visible via the console of javascript (browser)
>WriteLog($logFile,$txt)
It writes a log file and adds the txt to the log. If the log file is full (10mb) then it's emptied.
It's a class with a collection of functions related with strings.
> Text::getArgument($txt,[$set='='],[$trimValue=true])
Returns an array with the name of the argument and value (if any). It always returns a two dimension array
> Example Text::getArgument('alpha=hello') > ['alpha','hello']
> Example Text::getArgument('alpha:hello',':') > ['alpha','hello']
> Text::strPosNotSpace($txt,[$offset=0])
Returns the first position of a string that it's not a space
Text::strPosNotSpace(' abc def');
// returns 3
> isUpper($str)
Returns true if the str is (completelly) uppercase
> isLower($str)
Returns true if the str is (completelly) lowercase
> stripQuotes($text)
Strip quotes of a text (" or ')
Example:
Text::stripQuotes('"hello world"');
// returns hello world
> between($haystack, $startNeedle, $endNeedle,&$offset=0, $ignoreCase=false)
Returns the text between two needles.
> Text::between('Hello Brave World','Hello','World') // returns " Brave "
> replaceBetween($haystack, $startNeedle, $endneedle, $replaceText, &$offset=0,$$replaceTag=false)
Replace the text between two needles * If $replaceTag is true then it also replaces the $startNeedle and $endneedle
> Text::replaceBetween('Hello Brave World','Hello','World',' Wayne ') // returns "Hello Wayne World"
> removeFirstChars($txt,$length=1)
Remove the first character(s) for a string
> Text::removeFirstChars('Hello') // returns "ello"
> removeLastChars($txt,$length=1)
Remove the last character(s) for a string
> Text::removeLastChars('Hello') // returns "Hell"
It transforms a text = 'a1=1,a2=2' into an associative array. It uses the method parse_str() to do the conversion
> parseArg($text, $separator = ',')
Text::parseArg('a=1,b=1');
// returns ['a'=>'1','b'=>'1']
It parses a natural string and returns a declarative array. A "natural string", it is a set of values or arguments separated by space , where a value is the index and the new one is the value of the index.
> naturalArg($text, $separator = ',')
Text::naturalArg('select * from table where 1=1'
,['select'=>'req','from'=>'req','where'=>'opt']);
// returns ['select'=>'*','from'=>'table','where'=>'1=1']
Text::naturalArg('item export table inport file'
,['item'=>'first','export'=>'opt','inport'=>'opt']);
// returns: ['item' => 'item', 'export' => 'table', 'inport' => 'file']
Retains the case of the text minus the first letter that it's converted in lowercase.
Example:
Text::camelCase('HelloWorld');
// return "helloWorld";
Text::camelCase('hello_world');
// return "helloWorld";
It find the first (or last) ocurrence of a text. Unlikely strpos(), this method allows to find more than one neddle.
> function strposArray($haystack, $needles,$offset=0,$last=false)
Example:
Text::strposArray('a,b.d.e,f.g',['x','t','.']);
// return 3
Text::strposArray('a,b.d.e,f.g',['x','t',','],0,true);
// return 7
Remove the initial and final parenthesis but only if both matches. If the $start and $end arguments are arrays then both must have the same count and arrays are compared by pair of index
Example:
Text::removeParenthesis('hello');
// return "hello";
Text::removeParenthesis('(hello)');
// return "hello";
Text::removeParenthesis('[hello]'
,['(','{','[']
,[')','}',']']);
// returns "hello"
Text::removeParenthesis("'hello'"
,"'"
,"'");
// returns "hello"
Returns true if it has both parenthesis.
Example:
Text::hasParenthesis('hello');
// return false;
Text::hasParenthesis('(hello)');
// return true;
It adds parenthesis only if the original input does not have it.
Example:
Text::addParenthesis('hello');
// return '(hello)';
Text::addParenthesis('(hello)');
// return '(hello)';
Apache-2.0.
Files (16) |
File | Role | Description | ||
---|---|---|---|---|
docs (2 files) | ||||
examples (2 files) | ||||
lib (3 files) | ||||
test (4 files) | ||||
.travis.yml | Data | Auxiliary data | ||
composer.json | Data | Auxiliary data | ||
LICENSE | Lic. | License text | ||
phpunit.xml | Data | Auxiliary data | ||
README.md | Doc. | Documentation |
Files (16) | / | docs |
File | Role | Description |
---|---|---|
raccoon.png | Data | Auxiliary data |
raccoon_small.png | Icon | Icon image |
Files (16) | / | examples |
File | Role | Description |
---|---|---|
testCollection.php | Example | Example script |
testDebug.php | Example | Example script |
Files (16) | / | lib |
File | Role | Description |
---|---|---|
Collection.php | Class | Class source |
Debug.php | Class | Class source |
Text.php | Class | Class source |
Files (16) | / | test |
File | Role | Description |
---|---|---|
bootstrap.php | Aux. | Auxiliary script |
CollectionTest.php | Class | Class source |
DebugTest.php | Class | Class source |
TextTest.php | Class | Class source |
The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
Install with Composer |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.