Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#ActualNercury

Posted 26 February 2013 - 05:27 PM

You have it easy, you are not maintaining legacy PHP code.

This is direct copy-paste from code currently in production.

Guess what is it doing. Hint: in function name.

 

    function extract_where_from_join($on_condition) {
	$where_list = array();
	
	$anal_list = array('="' => '"', '= "' => '"', "='" => "'", "= '" => "'"); 
	
	$anal_pos = FALSE;
	$anal_end_pos = FALSE;
	$found_anal_from = FALSE;
	$found_anal_to = FALSE;
	foreach ($anal_list as $anal_from => $anal_to) {
	    $anal_pos = strpos($on_condition, $anal_from);
	    if ($anal_pos !== FALSE) {
		$found_anal_from = $anal_from;
		$anal_end_pos = strpos($on_condition, $anal_to, $anal_pos + strlen($anal_from));
		if ($anal_end_pos !== FALSE) {
		    $found_anal_to = $anal_to;
		    break;
		}
	    }
	}
	
	if ($anal_pos !== FALSE && $anal_end_pos !== FALSE) {
	    $anal_value = substr($on_condition, $anal_pos + strlen($found_anal_from), $anal_end_pos - $anal_pos - strlen($found_anal_from));
	    $condition = 'and';
	    $not_space_bw_pos = strbipos($on_condition, $condition, $anal_pos);
	    if ($not_space_bw_pos === FALSE) {
		$where_list[] = array(trim(substr($on_condition, 0, $anal_pos)), $anal_value);
		$on_condition = substr($on_condition, $anal_end_pos + strlen($found_anal_to));
		if ($on_condition === FALSE) {
		    $on_condition = 'TRUE';
		}
	    } else {
		$where_list[] = array(trim(substr($on_condition, $not_space_bw_pos + strlen($condition), $anal_pos - $not_space_bw_pos -strlen($condition))), $anal_value);
		$on_condition = substr($on_condition, 0, $not_space_bw_pos) . substr($on_condition, $anal_end_pos + strlen($found_anal_to));
		if ($on_condition === FALSE) {
		    $on_condition = 'TRUE';
		}
	    }
	}
	
	return array($where_list, $on_condition);
    }

 

It turns out this is parsing SQL join statement and extracting it's condition to be used elsewhere as "where" condition. Talking about easy way?


#1Nercury

Posted 26 February 2013 - 05:25 PM

You have it easy, you are not maintaining legacy PHP code.

This is direct copy-paste from code currently in production.

Guess what is it doing. Hint: in function name.

 

    function extract_where_from_join($on_condition) {
	$where_list = array();
	
	$anal_list = array('="' => '"', '= "' => '"', "='" => "'", "= '" => "'"); 
	
	$anal_pos = FALSE;
	$anal_end_pos = FALSE;
	$found_anal_from = FALSE;
	$found_anal_to = FALSE;
	foreach ($anal_list as $anal_from => $anal_to) {
	    $anal_pos = strpos($on_condition, $anal_from);
	    if ($anal_pos !== FALSE) {
		$found_anal_from = $anal_from;
		$anal_end_pos = strpos($on_condition, $anal_to, $anal_pos + strlen($anal_from));
		if ($anal_end_pos !== FALSE) {
		    $found_anal_to = $anal_to;
		    break;
		}
	    }
	}
	
	if ($anal_pos !== FALSE && $anal_end_pos !== FALSE) {
	    $anal_value = substr($on_condition, $anal_pos + strlen($found_anal_from), $anal_end_pos - $anal_pos - strlen($found_anal_from));
	    $condition = 'and';
	    $not_space_bw_pos = strbipos($on_condition, $condition, $anal_pos);
	    if ($not_space_bw_pos === FALSE) {
		$where_list[] = array(trim(substr($on_condition, 0, $anal_pos)), $anal_value);
		$on_condition = substr($on_condition, $anal_end_pos + strlen($found_anal_to));
		if ($on_condition === FALSE) {
		    $on_condition = 'TRUE';
		}
	    } else {
		$where_list[] = array(trim(substr($on_condition, $not_space_bw_pos + strlen($condition), $anal_pos - $not_space_bw_pos -strlen($condition))), $anal_value);
		$on_condition = substr($on_condition, 0, $not_space_bw_pos) . substr($on_condition, $anal_end_pos + strlen($found_anal_to));
		if ($on_condition === FALSE) {
		    $on_condition = 'TRUE';
		}
	    }
	}
	
	return array($where_list, $on_condition);
    }

 

It turns out this is parsing SQL query and extracting it's condition to be used elsewhere as "where" condition. Talking about easy way?


PARTNERS