CodeShareBeta

Simple solutions for common problems

Last codes
Generate random strings
Author: Kprkpr Version: 1 Lang: PHP
Description
Generate, from a strings var, a number of random ones.
Code
function randomString($length)
{
    $source = 'abcdefghijklmnopqrstuvwxyz1234567890';
    if($length>0){
        $result = "";
        $source = str_split($source,1);
        for($i=1; $i<=$length; $i++){
            //mt_srand((double)microtime() * 1000000); //Semilla, solo válido para Linux
            $num = mt_rand(1,count($source));
            $result.= $source[$num-1];
        }
    }
    return $result;
}
Random number in a range
Author: JoseluCross Version: 1 Lang: C/C++
Description
Generate a random number between two numbers
Code
int randomGen(int min, int max) {
  return (rand() / (1.0 + RAND_MAX) * (1 + max - min) + min);
}

Return http code of URL
Author: Kprkpr Version: 1 Lang: PHP
Description
This funcions returns the http code of a URL specified, or false if not valid (Can be adjusted).
Code
function httpCode($url, $wait = 5)
{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, TRUE);
		curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_TIMEOUT, $wait); //timeout in seconds
		$head = curl_exec($ch);
		$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		curl_close($ch);
		
		return $head ? $httpCode : FALSE; // This can be either FALSE or a number of standard error like 404
}
Launch a dialog in Android
Author: JoseluCross Version: 2 Lang: Java
Description
Method to launch a dialog based in DialogFragment
Code
private void launchDialog() {
 DialogFragment d = new MyDialog(); //From android.app.DialogFragment;
 //MyDialog is a implementation whith de dialog details
 d.setCancelable(false); //True if cancelable
 d.show(getFragmentManager(), "IDENTIFIER");
}
Where is midnight
Author: Kprkpr Version: 2 Lang: PHP
Description
Method to know where is now the 00:00 from the point of view of UTC±0
Code
$ho = date('H', strtotime('now')); //Hora en UTC
if ($ho == 0){$hu = 0;}
if ($ho >= 1 && $ho <= 11) {
	$hu =0-$ho;
}
if ($ho >= 13 && $ho <= 23) {
	$hu = 24-$ho;
}
//$hu es el huso horario a usar
Gnome sort
Author: JoseluCross Version: 1 Lang: Bash
Description
Implementation of gnome sort algorithm lower to higher
Code
function gnomeSort {
    local -i next
    for ((i=0;i<${#array[@]};i++)){
        let next=i+1
        if [ $next -ne ${#array[@]} ];then
            element1=${array[$i]}
            element2=${array[$next]}
            if [ $element1 -lt $element2 ];then
                swap=$element2
                array[$next]=$element1
                array[$i]=$swap
                if [ $i -ne 0 ];then
                    let i--
                    let i--
                fi
            fi
        fi
    }
}
Where is midnight
Author: JoseluCross Version: 3 Lang: Java
Description
Method to know where is now the 00:00 from the point of view of UTC±0
Code
int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
int timeZone; 
//Time zone where is midnight
if(hour == 0)
    timeZone=0;
else if(hour >= 1 && hour <= 11)
    timeZone = 0-hour;
else
    timeZone = 24-hour;
RTF Spanish tildes
Author: JoseluCross Version: 1 Lang: Python
Description
Replacement of special characters in Spanish with RTF-compatible characters
Code
def toRTF(text):
    middle = text
    middel = middle.replace("á", "\\'e1")
    middel = middel.replace("Á", "\\'c1")
    middel = middel.replace("é", "\\'e9")
    middel = middel.replace("É", "\\'c9")
    middel = middel.replace("í", "\\'ed")
    middel = middel.replace("Í", "\\'cd")
    middel = middel.replace("ó", "\\'f3")
    middel = middel.replace("Ó", "\\'d3")
    middel = middel.replace("ú", "\\'fa")
    middel = middel.replace("Ú", "\\'da")
    middel = middel.replace("ü", "\\'fc")
    middel = middel.replace("Ü", "\\'dc")
    middel = middel.replace("ñ", "\\'f1")
    middel = middel.replace("Ñ", "\\'d1")
    return middel
RTF Spanish tildes
Author: JoseluCross Version: 1 Lang: C#
Description
Replacement of special characters in Spanish with RTF-compatible characters
Code
private string sustituir(string rtf){
    string middel;
    middel = rtf.Replace("á", "\\'e1");
    middel = middel.Replace("Á", "\\'c1");
    middel = middel.Replace("é", "\\'e9");
    middel = middel.Replace("É", "\\'c9");
    middel = middel.Replace("í", "\\'ed");
    middel = middel.Replace("Í", "\\'cd");
    middel = middel.Replace("ó", "\\'f3");
    middel = middel.Replace("Ó", "\\'d3");
    middel = middel.Replace("ú", "\\'fa");
    middel = middel.Replace("Ú", "\\'da");
    middel = middel.Replace("ü", "\\'fc");
    middel = middel.Replace("Ü", "\\'dc");
    middel = middel.Replace("ñ", "\\'f1");
    middel = middel.Replace("Ñ", "\\'d1");
    return middel;
}