robertoandres24
8/9/2016 - 9:43 PM

Funciones Helpers

Funciones Helpers

    public function randomGenerator($qtd){ 
        //Under the string $Caracteres you write all the characters you want to be used to randomly generate the code. 
        $Caracteres = 'ABCDEFGHIJKLMOPQRSTUVXWYZ0123456789'; 
        $QuantidadeCaracteres = strlen($Caracteres); 
        $QuantidadeCaracteres--; 

        $Hash=NULL; 
            for($x=1;$x<=$qtd;$x++){ 
                $Posicao = rand(0,$QuantidadeCaracteres); 
                $Hash .= substr($Caracteres,$Posicao,1); 
            } 
        return $Hash; 
    } 
    // hace un str_replace y customize para q acepte 1 var o un array en 3er par
    public function modifyPath($old,$new,$paths){
        if (is_array($paths)) {
            $arrayNewPaths = [];
            foreach ($paths as $path) {
                $customPath = str_replace($old, $new, $path); //reemplazar el substring del path y queda
                $arrayNewPaths[] = $customPath;
            }
            return $arrayNewPaths;
        }
        else{
            $customPath = str_replace($old, $new, $paths);
        }
        return $customPath;
    }
  // busqueda segun determinado substring dentro de los items de un array
    public function ifExistsInArray($keyword, $arrayToSearch){
        foreach($arrayToSearch as $key => $arrayItem){
            if( stristr( $arrayItem, $keyword ) ){          //stristr()  insensible a mayusculas y minusculas
                return ['key'=> $key, 'value' => $arrayItem];
            }
        }
    }
    public function BannersActive()
    {
        return $this->hasMany('App\Ad')
                    ->where('user_id', $this->id)
                    ->where('status', 1)
                    ->get();
    }
<?php
 
$permitted_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
 
function generate_string($input, $strength = 16) {
    $input_length = strlen($input);
    $random_string = '';
    for($i = 0; $i < $strength; $i++) {
        $random_character = $input[mt_rand(0, $input_length - 1)];
        $random_string .= $random_character;
    }
 
    return $random_string;
}
 
// Output: iNCHNGzByPjhApvn7XBD
echo generate_string($permitted_chars, 20);