Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Usage:
- First param should be a field in a mysql query or a join condition
- $this->_stick_field('table.fieldname = table2.fieldname2');
- private function _stick_field($field)
- {
- if (!preg_match('/`/', $field) && ($field != '*') && $this->_auto_sticks)
- {
- // Match any table names associated with a field name.
- if (preg_match('/^[a-zA-Z0-9\-_\*]+\.[a-zA-Z0-9\-_\*]+$/', $field))
- {
- $_arr_field = explode(".", $field);
- foreach ($_arr_field as $k => $v)
- {
- if ($v != '*')
- {
- $_arr_field[$k] = '`'.$v.'`';
- }
- }
- return implode(".", $_arr_field);
- }
- /**
- * This is primarily used for join conditions.
- * The following tries to match as many table and field
- * names as posible, then splits them up, add backsticks
- * puts them togheter and replace the original string
- * with the formatted one.
- */
- preg_match_all('/([a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+)/', $field, $match);
- // Did we find any match?
- if (!empty($match[0]))
- {
- $_replace = array();
- // Search through the matches
- foreach ($match[0] as $k => $v) {
- $_arr_field = explode('.', $v);
- $_buffer = array();
- // Wrap field/table with backsticks
- foreach ($_arr_field as $value)
- {
- if ($value != '*')
- {
- // Add the formatted field/value to the buffer
- $_buffer[] = "`".$value."`";
- }
- }
- // Store the formatted field/value in the replace var
- $_replace[$k] = implode('.', $_buffer);
- }
- // Replace the original string with the formatted string
- return str_replace($match[0], $_replace, $field);
- } else {
- return '`'.$field.'`';
- }
- }
- return $field;
- } // _stick_field($field)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement