<?php
/**
* Tokenizes text that looks something like SQL.
*/
function tokenizeSQL( $SQL )
{
$functions = array ( 'concat', 'if' );
$token = '\\(|\\)|[\']|"|\140|[*]|,|<|>|<>|=|[+]';
$terminal = $token.'|;| |\\n';
$result = array();
$string = $SQL;
$string = ltrim($string);
$string = rtrim($string,';').';'; // always ends with a terminal
$string = preg_replace( "/[\n\r]/s", ' ', $string );
while(
preg_match( "/^($token)($terminal)/s", $string, $matches ) ||
preg_match( "/^({$token})./s", $string, $matches ) ||
preg_match( "/^([a-zA-Z0-9_.]+?)($terminal)/s", $string, $matches)
)
{
$t = $matches[1];
if ($t=='\'')
{
// it's a string
$t = tokSingleQuoteString( $string );
array_push($result, $t);
}
else if ($t=="\140")
{
// it's a backtick string (a name)
$t = tokBackQuoteString( $string );
array_push($result, $t);
}
else if ($t=='"')
{
// it's a double quoted string (a name in normal sql)
$t = tokDoubleQuoteString( $string );
array_push($result, $t);
}
else
{
array_push($result, $t);
}
$string = substr( $string, strlen($t) );
$string = ltrim($string);
}
return $result;
}
function tokSingleQuoteString( $string )
{
// matches a single-quoted string in $string
// $string starts with a single quote
preg_match('/^(\'.*?\').*$/s', $string, $matches );
return $matches[1];
}
function tokBackQuoteString( $string )
{
// matches a back-quoted string in $string
// $string starts with a back quote
preg_match('/^([\140].*?[\140]).*$/s', $string, $matches );
return $matches[1];
}
function tokDoubleQuoteString( $string )
{
// matches a back-quoted string in $string
// $string starts with a back quote
preg_match('/^(".*?").*$/s', $string, $matches );
return $matches[1];
}
print "<pre>";
print_r(tokenizeSQL('SELECT name, rew from asdf '));
print "</pre>";
- Output for git.master, git.master_jit, rfc.property-hooks
- <pre>Array
(
[0] => SELECT
[1] => name
[2] => ,
[3] => rew
[4] => from
[5] => asdf
)
</pre>
This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches
Archived branches
Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:
43 ms | 401 KiB | 8 Q