PHP Snippets
De drev
function html_to_plain($html){ $html = strip_tags($html); $html = html_entity_decode($html); $html = str_replace(array("\r","\n"), '', $html); return $html; }
function create_sql($fields, $tablename, $where='', $extra_fields = array(), $separator='_'){ $fields_list = array(); $values_list = array(); foreach($fields as $field_name => $field_value){ $match; //match tablename_fieldname from the form inputs preg_match("/(\w+)$separator(\w+)/", $field_name, $match); //add to the list only if the tablename from the //input matches the tablename to insert to. if(isset($match[1]) && isset($match[2]) && $match[1]==$tablename){ array_push($fields_list, $match[2]); array_push($values_list, "'".addslashes($field_value)."'"); //push with single quotes and slashes } } //generate the sql string to return, UPDATE OR INSERT $sql; if($where == ''){ //Generate an INSERT $str_fields = implode(', ', $fields_list); $str_values = implode(', ', $values_list); //add extra fields if($extra_fields){ $str_fields .= ', '.implode(', ', array_keys($extra_fields)); $str_values .= ', '.implode(', ',array_values($extra_fields)); } $sql = "INSERT INTO $tablename ( $str_fields ) VALUES ( $str_values );"; } else{ //Generate an UPDATE $sql = "UPDATE $tablename SET "; for($i=0;$i<count($fields_list);$i++){ $sql .= $fields_list[$i] . ' = '. $values_list[$i].','; } //add extra fields if($extra_fields){ foreach ($extra_fields as $extra_field => $extra_value){ $sql .= "$extra_field = '$extra_value',"; } } //remove trailing comma $sql = substr($sql, 0, -1); //Add WHERE clause $sql .= ' WHERE '.$where; } return $sql; }