Skip to content

Instantly share code, notes, and snippets.

@robertoandres24
Last active January 24, 2019 19:32
Show Gist options
  • Save robertoandres24/1db4278ae54d6c38132c34634bd2d459 to your computer and use it in GitHub Desktop.
Save robertoandres24/1db4278ae54d6c38132c34634bd2d459 to your computer and use it in GitHub Desktop.
Funciones Helpers
public function BannersActive()
{
return $this->hasMany('App\Ad')
->where('user_id', $this->id)
->where('status', 1)
->get();
}
// 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];
}
}
}
// 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;
}
<?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);
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment