Juan Garcés

Personal Blog

Replace With VIM

August 17th, 2014

Many times I have seen the need to replace text with VIM. Here I leave the way to do.

First, we open the file to edit:


vim file

We press ESC and insert colon “:”. After that, we insert the instruction:


:1,$ s/search_text/replacement_text/g

The characters 1,$ indicate that should search from the first to the last line of the file. After, just replace “search_text” and “replacement_text” for that we need.

Connect MySQL with PDO

August 17th, 2014

Connection to MySQL using PDO library.

CREATE TABLE `paciente` (
  `paciente_rut` varchar(12) NOT NULL,
  `paciente_nombre` varchar(100) NOT NULL,
  `paciente_apellidop` varchar(100) NOT NULL,
  `paciente_apellidom` varchar(100) default NULL,
  `paciente_fono` varchar(50) default NULL,
  `paciente_direccion` varchar(255) NOT NULL,
  PRIMARY KEY  (`paciente_rut`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `paciente` VALUES ('11111111-1', 'Pedro', 'Fuentes', 'García', NULL, 'Santiago');
INSERT INTO `paciente` VALUES ('22222222-2', 'Juan', 'Santana', 'Bustos', NULL, 'Santiago');
INSERT INTO `paciente` VALUES ('33333333-3', 'Carolina', 'Soto', 'Barra', NULL, 'Santiago');

Crypt with SHA1

August 9th, 2014

Method to crypt with SHA1

/// 
/// Crypt the string with SHA1
/// 
public static string EncriptarSHA1(string sCadena)
{
	System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1Managed.Create();

	ASCIIEncoding encoding = new ASCIIEncoding();
	byte[] stream = null;
	StringBuilder codificada = new StringBuilder();
	stream = sha1.ComputeHash(encoding.GetBytes(sCadena));

	for (int i = 0; i < stream.Length; i++) codificada.AppendFormat("{0:x2}", stream[i]);
	return codificada.ToString();
}

Arrays with SPL

August 9th, 2014

Using SPL to work with an array.

/* Arreglo */
$personas = array("Carlos", "Carolina", "José",
                  "Manuel", "Sandra");

try
{
	/* Creamos el objeto SPL */
	$arrayObjSPL = new ArrayObject($personas);

	/* Modificamos el elemento 4 */
	$arrayObjSPL->offsetSet(4, "Claudia");

	/* Eliminamos el elemento 2 */
	$arrayObjSPL->offsetUnset(2);

	/* Iteramos sobre el arreglo con el objeto SPL */
	for($iterator = $arrayObjSPL->getIterator();	$iterator->valid();	$iterator->next())
	{
		/* Mostramos el elemento */
		echo $iterator->key() . " -> " . $iterator->current() . "
"; } } catch (Exception $e) { echo $e->getMessage(); }

C# isNumeric

August 9th, 2014

Visual Basic IsNumeric method for C#.


/// Check if obj is numeric
public static bool IsNumeric(object obj)
{
	double retNum;
	bool isNum = Double.TryParse(Convert.ToString(obj),
								 System.Globalization.NumberStyles.Any,
								 System.Globalization.NumberFormatInfo.InvariantInfo,
								 out retNum);
	return isNum;
}

Juan Garcés

Personal Blog