Skip to content

Instantly share code, notes, and snippets.

@Nodws
Last active March 27, 2024 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nodws/ded4a07a84f8f6b6df380c8980672fc8 to your computer and use it in GitHub Desktop.
Save Nodws/ded4a07a84f8f6b6df380c8980672fc8 to your computer and use it in GitHub Desktop.
<?
add_filter( 'the_content', function($content){
if(!is_admin() ){
return preg_replace_callback('~\[proveedor=(.*?)\]~iU', function($m){
$id = str_replace(['/','"',"'"],'',$m[1]);
$args = array(
// 'name' => $id,
'post_type' => 'gd_place',
'post_status' => 'publish',
'numberposts' => 1
);
if(is_numeric($id))
$args['p'] = $id;
else
$args['name'] = $id;
$my_posts = get_posts($args);
if( $my_posts )
{ $p = $my_posts[0];
return "<a href='/proveedor/{$p->post_name}' class=popen>{$p->post_title}</a>";}
}, $content);
}
<?php
//remove accents
setlocale(LC_ALL, "en_US.utf8");
function clean($txt){
$txt= iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $txt);
$re = "/[^\\w.-]/";
return preg_replace($re, "", $txt);
}
<?php
function comp ($string){
return strtr(base64_encode(addslashes(gzcompress(serialize($string),9))), '+/=', '-_,');
}
function expa ($string){
return unserialize(gzuncompress(stripslashes(base64_decode(strtr($string, '-_,', '+/=')))));
}
$s = 'administrator';
$string = bin2hex($s); //hex works best for single words
$coded = base64_encode($s); //b64 works best for numbers and text
<?
$percent = substr($num, 0, ((strpos($num, '.')+1)+2)); //9.99
<?php
function is_empty($var){
if (isset($var) === false || $var === false || $var === null || $var === -0.0 || $var === '' || $var === [])
return true;
return false;
}
//usage
$ble = 0; //0 is NOT empty
echo is_empty($ble) ? "Empty" :"Not Empty: $ble";
<?
function ext($f){
return substr($f, strrpos($f, ".") + 1);
}
<?
$csv = explode("\n", file_get_contents('http://test.csv'));
foreach ($csv as $key => $line)
{
$csv[$key] = str_getcsv($line);
}
<?php
$d = 'path/to/images/';
foreach(glob($d.'*.{jpg,JPG,jpeg,JPEG,png,PNG}',GLOB_BRACE) as $file){
$imag[] = basename($file);
}
<?
$map = [
'Red Post' => 'Awesome Post',
'Green Post' => 'Crazy Post',
'Blue Post' => 'Insane Post'
];
$data = str_replace( array_keys( $map ), $map, $data );
<?
$url = 'http://example.com';
$method = 'read';
if($method != 'read')
{ $html = file_get_contents($url); }
else
{ ob_start(); readfile($url); $html = ob_get_clean(); }
$doc = new DomDocument;
$doc->validateOnParse = true;
$doc->loadHtml($html);
$output = clean($doc->getElementByClass('container')->textContent);
echo $output;
<?
//wordpress-like slug
function wslug($text)
{
return strtolower(trim(preg_replace(['/[^a-z0-9]+/i', '/-+/'], ['-', '-'], iconv('UTF-8', 'ASCII//TRANSLIT', $text)), '-'));
}
//react like slug
function rslug($input) {
$input = iconv('UTF-8', 'ASCII//TRANSLIT', $input);
$input = preg_replace('/[^a-zA-Z0-9-]+/', '-', $input);
$input =trim(preg_replace('/-+/', '-', strtolower($input)), '-');
return $input;
}
//tinyurl slug uuid
function tslug($path){
return hash('crc32', $path, FALSE);
}
<?
$to = 'test@test.com';
$subject = 'The test for php mail function';
$message = 'Hello';
$headers = 'From: test@test.com' . "\r\n" .
'Reply-To: test@test.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$success = mail($to, $subject, $message, $headers);
if ( isset( $success ) )
echo 'E-mail sent to: ' . $to . '<br /> Successful mail?: <strong ' . ( $success ? 'style="color:green;">YES' : 'style="color:red;">NO' ) . '</strong>';
?>
<?php
function texto($text, $limit=8) {
$text = strip_tags($text);
if (str_word_count($text, 0) > $limit) {
$words = str_word_count($text, 2);
$pos = array_keys($words);
$text = substr($text, 0, $pos[$limit]) . '...';
}
return $text;
}
<?
function trunc($txt,$ln=12) {
$t = substr($txt, 0, $ln);
return strlen($txt)>$ln ? $t.'...' : $t;
}
<?php
function urlsafe( $input ){
$hash = base64_encode( $input );
// Replace non-urlsafe chars to make the string urlsafe.
$hash = strtr( $hash, '+/', '-_' );
// Trim base64 padding characters from the end.
$hash = rtrim( $hash, '=' );
return $hash;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment