I have set up code that will find whatever keywords I specify in a SQL query, specifically SELECT, FROM and WHERE. I can even do something when it exists. What I want to do is use SELECT, FROM and WHERE as delimiters.
In an earlier post, I asked about getting the data out. Now, how could I search 'between' the delimiters, WITHOUT including the delimiters themselves?
Why do I want to do this? I eventually want 'explode' the stuff between SELECT and FROM, FROM and SELECT, and SELECT and WHERE.
Example:
Array ( [0] => SELECT [1] => placement_primarytech.PrimaryTechnology, [2] => placement_candidate.FirstName, [3] => placement_candidate.LastName [4] => FROM [5] => placement_primarytech, [6] => placement_candidate [7] => WHERE [8] => placement_primarytech.PrimaryTechnology [9] => LIKE [10] => "%c%" [11] => AND [12] => placement_candidate.FirstName [13] => = [14] => "joe" [15] => AND [16] => placement_candidate.LastName [17] => = [18] => "blow" )
I'll want to explode so that $new_searchkeyFrom will tell me:
placement_primarytech
placement_candidate
(and from there I can construct a dynamic query based on existing fields)
Code:
$sql2 = $sql;
$chars = preg_split('/ /', $sql2, -1, PREG_SPLIT_DELIM_CAPTURE);
print_r($chars);
echo "<P>";
function array_search_bit($search, $array_in)
{
foreach ($array_in as $key => $value)
{
if (strpos($value, $search) !== FALSE)
return $key;
}
return FALSE;
}
$searchkeySelect = array_search_bit("SELECT", $chars);
$searchkeyFrom = array_search_bit("FROM", $chars);
$searchkeyWhere = array_search_bit("WHERE", $chars);
if (isset($searchkeySelect)) {
echo "The 'SELECT' element is in the array<BR>";
}
if (isset($searchkeyFrom)) {
echo "The 'FROM' element is in the array<BR>";
}
if (isset($searchkeyWhere)) {
echo "The 'WHERE' element is in the array<BR>";
}