Skip to content

Instantly share code, notes, and snippets.

@waelio
waelio / The_Slug.php
Last active December 29, 2015 13:14
Get part of the slug from the url
<?php
/**
* @param int $position
* @param bool|true $echo
* @return bool
*/
function the_slug($position=1,$echo=false){
$url = parse_url($_SERVER['REQUEST_URI']);
$slugs = array_filter(explode('/',$url['path']));
@waelio
waelio / Inject_Arrays.php
Last active December 29, 2015 13:14
Inject an array with another array a certainposition
<?php
/**
* @param array $needle
* @param array $haystack
* @param $position
* @return array
*/
function inject_this_array($needle=array(),$haystack=array(),$position){
$new_holder1 = array_slice($haystack, 0, $position, true);
$new_holder2 = array_slice($haystack, $position, count($haystack)-1,true);
@waelio
waelio / get_object_type.php
Last active December 29, 2015 13:15
WP- Return the object type
<?php
/**
* @param object|int|string $object
* @return string
*/
function get_object_type($object){
$workable = false;
$object_type = gettype($object);
switch($object_type){
@waelio
waelio / Class_Element.php
Last active December 29, 2015 13:13
PHP - Create html elements
<?php
/**
* Create an element
*/
class Element
{
private $type;
private $unaryTagArray = array('area','base','br','col','command','embed','hr','img','input','keygen','link','meta','param','source','track','wbr');
private $attributes;
private $innerHtml;
@waelio
waelio / get_avatar_link.php
Last active December 29, 2015 13:12
Get the HREF(src) on an <img />
@waelio
waelio / HTML_Tables_from_MySQL_Database.php
Last active December 29, 2015 13:12
Create HTML Table (header/body/footer) from MySQL Database using PDO
<?php
function getConnection1(){
$dbhost = "localhost";
$dbuser = "test";
$dbpass = "test";
$dbname = "schema name";
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbh;
}
@waelio
waelio / Random_Password.php
Last active December 29, 2015 13:11
Random Password from predefined dictionary
<?php
function generate_password($length = 64){
$box_array = array( 'jhg','RTJ','ZCG','{;G','KRA','!','@','$','%','^','&','*','(',')','_','=','+' );
$password='';
for($x=0; $x < $length; $x++){
$password .= $box_array[intval(array_rand($box_array))];
}
@waelio
waelio / Limit_string.php
Last active December 29, 2015 13:10
limiting string length by number of charachters while preserving whole words
<?php
/**
* replace array Values (string) with length values
* @param array $string
*/
function get_string_length(&$string){
if ((is_string($string)) || (is_integer($string)))
$string = strlen($string);
}
@waelio
waelio / clean_wp.php
Last active December 29, 2015 13:06
Remove unreal images from wp
<?php
$imgs = get_posts("post_type=attachment&numberposts=-1");
$x=0;
foreach($imgs as $img){
$file = get_attached_file($img->ID);
$Image_size = getimagesize($file);
if(!is_array($Image_size)){
wp_delete_post( $img->ID, false );
echo $x .'- Deleting attachment #'.$img->ID;
@waelio
waelio / Entity.php
Last active August 13, 2021 08:31
autonymous object
<?php
/**
* Created by PhpStorm.
* User: wahbehw
* Date: 2/1/2016
* Time: 9:26 AM
*/
//namespace entity_object;