Category: PHP

PHP Functions

How to Sort an Array with PHP

Create the function sortArray() public function sortArray(&$array, $subkey=”id”, $sort_ascending=true) { if (count($array)) $temp_array[key($array)] = array_shift($array); foreach($array as $key => $val){ $offset = 0; $found = false; foreach($temp_array as $tmp_key => $tmp_val) { if(!$found and strtolower($val[$subkey]) > strtolower($tmp_val[$subkey])) { $temp_array = array_merge( (array)array_slice($temp_array,0,$offset), array($key => $val), array_slice($temp_array,$offset) ); $found = true; } $offset++; } if(!$found) $temp_array […]

Read more
PHP Functions

Plesk PHP Version Not Matching Command Line

echo “PATH=/opt/plesk/php/7.1/bin:$PATH” >> ~/.bash_profile Replace the version number with the one you have set. Then close out the terminal and reopen.  Run php -v and you should see the correct version.   php -v PHP 7.4.27 (cli) (built: Dec 23 2021 14:48:17) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) […]

Read more
PHP Functions

Randomly Generate Readable Password

function auth_pwgen(){ $pw = ”; $c = ‘bcdfghjklmnprstvwz’; //consonants except hard to speak ones $v = ‘aeiou’; //vowels $a = $c.$v; //both //use two syllables… for($i=0;$i < 2; $i++){ $pw .= $c[rand(0, strlen($c)-1)]; $pw .= $v[rand(0, strlen($v)-1)]; $pw .= $a[rand(0, strlen($a)-1)]; } //... and add a nice number $pw .= rand(10,99); return $pw; }

Read more

Disable Payment Method Based on Country Woocommerce PHP

// Disable gateway based on country function payment_gateway_disable_country( $available_gateways ) { global $woocommerce; if ( isset( $available_gateways[‘cod’] ) && $woocommerce->customer->get_country() <> ‘IN’ ) { unset( $available_gateways[‘cod’] ); } return $available_gateways; } add_filter( ‘woocommerce_available_payment_gateways’, ‘payment_gateway_disable_country’ );

Read more
PHP Functions

Woocommerce List all Available Payment Gateways

$available_gateways = WC()->payment_gateways->get_available_payment_gateways();

Read more

Get Youtube Thumbnails with PHP or Html

Each YouTube video has 4 generated images. They are predictably formatted as follows: http://img.youtube.com/vi/<insert-youtube-video-id-here>/0.jpg http://img.youtube.com/vi/<insert-youtube-video-id-here>/1.jpg http://img.youtube.com/vi/<insert-youtube-video-id-here>/2.jpg http://img.youtube.com/vi/<insert-youtube-video-id-here>/3.jpg The first one in the list is a full size image and others are thumbnail images. The default thumbnail image (ie. one of 1.jpg, 2.jpg, 3.jpg) is: http://img.youtube.com/vi/<insert-youtube-video-id-here>/default.jpg For the high quality version of the thumbnail use a url similar […]

Read more
PHP Functions

How to a Print to a Local or Network Printer with PHP and Linux Command Line

//Easy way to print from php or just plain command line. exec(“lpr [ options ] [ filename … ]”); //Gettinga little Fancy cat thesis.txt | lpr //Another example exec(“lpr -P printer -r filename.txt”); Read more in at this Link

Read more
PHP Functions

How to Split an Array into a String with PHP

$myArray = explode(‘,’, $myString);

Read more
PHP Functions

PHP Code for Replacing Characters in a String or Variable

str_replace(‘”‘, “”, $string); preg_replace(“//”, “”, $string); C-style quotes: preg_replace(“/\/\/.*?\n/”, “\n”, $string); CSS-style quotes: preg_replace(“/\/*.*?\*\//”, “”, $string); bash-style quotes: preg-replace(“/#.*?\n/”, “\n”, $string);

Read more
PHP Functions

Split PHP String into Multiple Variables Made Easy

Like a Glove….

Read more