Core Function GMatch
(→Example) |
(→Example) |
||
Line 42: | Line 42: | ||
$t = array(); | $t = array(); | ||
$s = "Hello world from Sputnik!"; | $s = "Hello world from Sputnik!"; | ||
− | foreach(gmatch($s, "(%w+)") as | + | foreach(gmatch($s, "(%w+)") as $v) |
{ | { | ||
$t[$k] = $v; | $t[$k] = $v; |
Revision as of 09:18, 9 August 2014
GMatch( <string>, <pattern>, <asKeypair>, <offset> )
Contents |
Description
Extract the patterns from a string and return as array or associative array .
Parameters
string
The string to evaluate.
pattern
Go read the Patterns section on the Find( ) function to learn how to create and use patterns then come back here to put it to use on this function.
asKeypair
Optional; If true the return will be an associative array where the first capture is the key and the second capture is the value (regardless of how many captures there are) if there are more than two captures then the third one will also become a key and forth one a value and so on.
offset
Optional; Specifies where to start the search; its default value is 0 and can be negative.
Return Value
Returns an array of all captures or optionally places the captures into key/value pairs if asKeypair is set to true.
Remarks
This function is pretty much the same a the LUA String.GMatch() however this one returns start position starting at 0 (LUA's starts at 1) and lowers the end position by 1, Also the Offset begins at 0 here where as in LUA it begins at 1.
This is because in Sputnik chars in a string start at 0 not 1.
Example
Basic example
$t = array(); $s = "Hello world from Sputnik!"; foreach(gmatch($s, "(%w+)") as $v) { $t[$k] = $v; } printr $t; // Prints // Array // ( // [0] => Hello // [1] => world // [2] => from // [3] => Sputnik // )
Example of using asKeypair in a Foreach loop
$t = array(); $s = "from=world, to=Sputnik"; foreach(gmatch($s, "(%w+)=(%w+)", true) as $k => $v) { $t[$k] = $v; } printr $t; // Prints // Array // ( // [from] => world // [to] => Sputnik // )