function project_find_alias($query, $table) { // See if {$table} is already in the query. $match = array(); // This regexp handles many cases: // - {node} can be either in FROM or in a JOIN clause // - Query might end immediately after {node} // - Might not have a table alias for {node} $pattern = "@.*(FROM|JOIN)\s+\{$table\}\s*(\S+)?@"; if (preg_match($pattern, $query, $match)) { $keywords = '@(ON|INNER|LEFT|RIGHT|WHERE|ORDER|GROUP|HAVING|LIMIT)@'; if (!isset($match[2]) || preg_match($keywords, $match[2])) { // No alias found, just use "node". $alias = $table; } else { $alias = $match[2]; } } else { $alias = FALSE; } return $alias; }