Snippets Collections
function replace_accents($str) {
   $str = htmlentities($str, ENT_COMPAT, "UTF-8");
   $str = preg_replace('/&([a-zA-Z])(uml|acute|grave|circ|tilde);/','$1',$str);
   return html_entity_decode($str);
}
function extract_emails_from($string) {
         preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $string, $matches);
         return $matches[0];
}
<?php

  $var1 = 'nameOfVariable';

  $nameOfVariable = 'This is the value I want!';

  echo $$var1; 

?>
&copy; <?php echo date("Y"); ?>
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
     $ip=$_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
     $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
     $ip=$_SERVER['REMOTE_ADDR'];
}
<?php 
  
function startsWith($string, $startString) { 
  $len = strlen($startString); 
  return (substr($string, 0, $len) === $startString); 
} 

// usage
echo startsWith("cat", "c"); // true
echo startsWith("dog", "x"); // false

?> 
SELECT * FROM tableName WHERE timeColumn > UNIX_TIMESTAMP( date_sub(now(),interval 1 week)) and UserName = 'givenUserName'
<!DOCTYPE html>
<html>

<head>
    <title>My MySQL Form</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
</head>

<body>
<?php

require 'helpers/connect.php';
$form = "<form>";

$result = $conn->query("describe codewalldb.people");

if ($result->num_rows > 0) {
    // output data of each row
    while ($row = $result->fetch_assoc()) {
        if ($row["Field"] != "id") {
            $form .= "<div class='form-group'>";
            $form .= "<label for='" . $row["Field"] . "'>" . $row["Field"] . "</label> <input type='text' class='form-control' name='" . $row["Field"] . "' >";
            $form .= "</div>";
        }
    }
    $form .= "<input type='submit' value='Submit'></form>";
}

echo $form;
?>

</body>

</html>
Copy
$servername = "localhost";
$username = "codewall";
$password = "password";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
Copy
add_action( 'wp_enqueue_scripts', function() { wp_dequeue_style( 'font-awesome' ); }, 50 );
add_action( 'elementor/frontend/after_enqueue_styles', function () { wp_dequeue_style( 'font-awesome' ); } );
/**
 * Change number of products that are displayed per page (shop page)
 */
add_filter( 'loop_shop_per_page', 'new_loop_shop_per_page', 20 );

function new_loop_shop_per_page( $cols ) {
  // $cols contains the current number of products per page based on the value stored on Options -> Reading
  // Return the number of products you wanna show per page.
  $cols = 9;
  return $cols;
}
<?php 
    
    $rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
    $color = '#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];
    
?>
<?php
  $gravatar_link = 'http://www.gravatar.com/avatar/' . md5($comment_author_email) . '?s=32';
   echo '<img src="' . $gravatar_link . '" />';
?>
function replace_accents($str) {
   $str = htmlentities($str, ENT_COMPAT, "UTF-8");
   $str = preg_replace('/&([a-zA-Z])(uml|acute|grave|circ|tilde);/','$1',$str);
   return html_entity_decode($str);
}
<?php 
  
function startsWith($string, $startString) { 
  $len = strlen($startString); 
  return (substr($string, 0, $len) === $startString); 
} 

// usage
echo startsWith("cat", "c"); // true
echo startsWith("dog", "x"); // false

?> 
function encrypt($key, $plain, $salt = null)
    {
        echo $key;
    $plain = serialize(array( $plain, $salt ));

    $crypt = mcrypt_module_open('rijndael-256', '', 'ofb', '');

    if (false !== stripos(PHP_OS, 'win') and 
        version_compare(PHP_VERSION, '5.3.0')  == -1) 
    {
        $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($crypt), MCRYPT_RAND);    
    }
    else
    {
        $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($crypt), MCRYPT_DEV_URANDOM);
    }

    $ks = mcrypt_enc_get_key_size($crypt);

    $key = substr(md5($key), 0, $ks);
    echo strlen($key);
    mcrypt_generic_init($crypt, $key, $iv);
    $encrypted = base64_encode($iv . mcrypt_generic($crypt, $plain));
    mcrypt_generic_deinit($crypt);
    mcrypt_module_close($crypt);

    return $encrypted;
}
<?php
  $gravatar_link = 'http://www.gravatar.com/avatar/' . md5($comment_author_email) . '?s=32';
   echo '<img src="' . $gravatar_link . '" />';
?>
function startsWith($string, $startString) {
  return strncmp($string, $startString, strlen($startString)) === 0;
}
<?php

add_filter( 'gettext', 'woocommerce_rename_coupon_field_on_cart', 10, 3 );
add_filter( 'gettext', 'woocommerce_rename_coupon_field_on_cart', 10, 3 );
add_filter('woocommerce_coupon_error', 'rename_coupon_label', 10, 3);
add_filter('woocommerce_coupon_message', 'rename_coupon_label', 10, 3);
add_filter('woocommerce_cart_totals_coupon_label', 'rename_coupon_label',10, 1);
add_filter( 'woocommerce_checkout_coupon_message', 'woocommerce_rename_coupon_message_on_checkout' );


function woocommerce_rename_coupon_field_on_cart( $translated_text, $text, $text_domain ) {
	// bail if not modifying frontend woocommerce text
	if ( is_admin() || 'woocommerce' !== $text_domain ) {
		return $translated_text;
	}
	if ( 'Coupon:' === $text ) {
		$translated_text = 'Voucher:';
	}

	if ('Coupon has been removed.' === $text){
		$translated_text = 'Voucher has been removed.';
	}

	if ( 'Apply coupon' === $text ) {
		$translated_text = 'Apply voucher';
	}

	if ( 'Coupon code' === $text ) {
		$translated_text = 'Voucher';
	
	} 

	return $translated_text;
}


// rename the "Have a Coupon?" message on the checkout page
function woocommerce_rename_coupon_message_on_checkout() {
	return 'Have a voucher?' . ' ' . __( 'Enter your voucher code', 'woocommerce' ) . '';
}


function rename_coupon_label($err, $err_code=null, $something=null){

	$err = str_ireplace("Coupon","Voucher ",$err);

	return $err;
}

?>
// assuming file.zip is in the same directory as the executing script.
$file = 'file.zip';

// get the absolute path to $file
$path = pathinfo(realpath($file), PATHINFO_DIRNAME);

$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
  // extract it to the path we determined above
  $zip->extractTo($path);
  $zip->close();
  echo "WOOT! $file extracted to $path";
} else {
  echo "Doh! I couldn't open $file";
}
DB::transaction(function () {
    DB::table('users')->update(['votes' => 1]);

    DB::table('posts')->delete();
}, 5);
<?php
/**
 * Template Name: Archive Album
 * Album Post type archive page.
 * *
 * @package Avada
 * @subpackage Templates
 */

// Do not allow directly accessing this file.
if ( ! defined( 'ABSPATH' ) ) {
	exit( 'Direct script access denied.' );
}
?>
<?php get_header(); ?>
<section id="content" <?php Avada()->layout->add_class( 'content_class' ); ?> <?php Avada()->layout->add_style( 'content_style' ); ?>>
	<?php if ( category_description( 'album' ) ) : ?>
		<div id="post-<?php the_ID(); ?>" <?php post_class( 'fusion-archive-description' ); ?>>
			<div class="post-content">
				<?php echo category_description(); ?>
			</div>
		</div>
	<?php endif; ?>

	<?php get_template_part( 'templates/blog', 'layout' ); ?>
</section>
<?php do_action( 'avada_after_content' ); ?>
<?php
get_footer();

/* Omit closing PHP tag to avoid "Headers already sent" issues. */
/**
 * Add Album post type
 * 
 */
function create_posttype() {
 
    register_post_type( 'album',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'album' ),
                'singular_name' => __( 'album' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'album'),
        )
    );
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );

/**
 * Show Album post type with Blog pages
 * 
 */
add_filter( 'pre_get_posts', 'my_get_posts' );

function my_get_posts( $query ) {

	if ( is_home() && $query->is_main_query() )
		$query->set( 'post_type', array( 'post', 'album') );

	return $query;
}
UPDATE TABLE_NAME
SET COL_NAME = REPLACE(COL_NAME, 'OLD_URL/', 'NEW_URL/') where 1
add_filter( 'woocommerce_available_payment_gateways', 'conditionally_disable_cod_payment_method', 10, 1);
function conditionally_disable_cod_payment_method( $gateways ){
    // HERE define your Products IDs
    $products_ids = array(2880);

    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $cart_item ){
        // Compatibility with WC 3+
        $product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->id : $cart_item['data']->get_id();
        if (in_array( $cart_item['product_id'], $products_ids ) ){
            unset($gateways['cod']);
            break; // As "COD" is removed we stop the loop
        }
    }
    return $gateways;
}
$myDateTime = DateTime::createFromFormat('Y-m-d', $dateString);
$newDateString = $myDateTime->format('m/d/Y');
<?php 
function count_num_finger( $n ) 
{ 
	$r = $n % 8; 
	if ($r == 1) 
		return $r; 
	if ($r == 5) 
		return $r; 
	if ($r == 0 or $r == 2) 
		return 2; 
	if ($r == 3 or $r == 7) 
		return 3; 
	if ($r == 4 or $r == 6) 
		return 4; 
}	 

// Driver Code 
$n = 30; 
echo(count_num_finger($n)); 
 
?> 
<?php 
// PHP program to find nth 
// magic number 

// Function to find nth 
// magic number 
function nthMagicNo($n) 
{ 
	$pow = 1; 
	$answer = 0; 

	// Go through every bit of n 
	while ($n) 
	{ 
	$pow = $pow * 5; 

	// If last bit of n is set 
	if ($n & 1) 
		$answer += $pow; 

	// proceed to next bit 
	$n >>= 1; // or $n = $n/2 
	} 
	return $answer; 
} 

// Driver Code 
$n = 5; 
echo "nth magic number is ", 
	nthMagicNo($n), "\n"; 

// This code is contributed by Ajit. 
?> 
$path = $_FILES['image']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);
bookmark
dashboard

Wed Jun 24 2020 09:51:02 GMT+0000 (UTC) https://css-tricks.com/snippets/php/convert-accented-characters/

#php
bookmark
dashboard

Mon Jun 22 2020 13:14:31 GMT+0000 (UTC) https://css-tricks.com/snippets/php/extract-email-addresses/

#php
bookmark
dashboard

Mon Jun 22 2020 13:13:18 GMT+0000 (UTC) https://css-tricks.com/snippets/php/variable-variables/

#php
bookmark
dashboard

Mon Jun 22 2020 13:09:02 GMT+0000 (UTC) https://css-tricks.com/snippets/php/automatic-copyright-year/

#php
bookmark
dashboard

Mon Jun 22 2020 13:02:38 GMT+0000 (UTC) https://css-tricks.com/snippets/php/get-users-ip-address/

#php
bookmark
dashboard

Mon Jun 22 2020 13:02:05 GMT+0000 (UTC) https://css-tricks.com/snippets/php/test-if-string-starts-with-certain-characters-in-php/

#php
bookmark
dashboard

Sun Jun 21 2020 13:31:54 GMT+0000 (UTC) https://stackoverflow.com/questions/9632392/get-mysql-database-entries-within-last-week

#mys #php
bookmark
dashboard

Sat Jun 06 2020 15:29:35 GMT+0000 (UTC) https://www.codewall.co.uk/how-to-automatically-make-a-mysql-input-form-with-php/?utm_source=CWTwitter

#php
bookmark
dashboard

Sat Jun 06 2020 15:23:27 GMT+0000 (UTC) https://www.codewall.co.uk/how-to-automatically-make-a-mysql-input-form-with-php/?utm_source=CWTwitter

#php
bookmark
dashboard

Fri Jun 05 2020 20:36:29 GMT+0000 (UTC) https://ltheme.com/how-to-quicken-a-slow-site-with-elementor-page-builder/

#php
bookmark
dashboard

Fri Jun 05 2020 06:35:17 GMT+0000 (UTC) https://makersbyte.com/how-to-change-number-of-products-display-in-woocommerce-product-listing-page/

#php
bookmark
dashboard

Tue Jun 02 2020 03:52:39 GMT+0000 (UTC) https://css-tricks.com/snippets/php/random-hex-color/

#php
bookmark
dashboard

Mon Jun 01 2020 05:59:54 GMT+0000 (UTC) https://css-tricks.com/snippets/php/display-a-user-gravatar-from-email-address/

#php
bookmark
dashboard

Mon Jun 01 2020 05:58:28 GMT+0000 (UTC) https://css-tricks.com/snippets/php/convert-accented-characters/

#php
bookmark
dashboard

Fri May 29 2020 13:09:12 GMT+0000 (UTC) https://css-tricks.com/snippets/php/test-if-string-starts-with-certain-characters-in-php/

#javascript #php
bookmark
dashboard

Fri May 29 2020 11:29:28 GMT+0000 (UTC) https://stackoverflow.com/questions/62083689/how-to-decrypt-rijndael-256-in-python-used-in-quickbooks-php-api

#php
bookmark
dashboard

Fri May 29 2020 10:09:17 GMT+0000 (UTC) https://css-tricks.com/snippets/php/display-a-user-gravatar-from-email-address/

#php
bookmark
dashboard

Fri May 29 2020 10:07:44 GMT+0000 (UTC) https://css-tricks.com/snippets/php/test-if-string-starts-with-certain-characters-in-php/

#javascript #php
bookmark
dashboard

Thu May 28 2020 22:22:31 GMT+0000 (UTC) https://www.grahamethomson.com/2018/10/07/rename-coupon-code-text-in-woocommerce/

#php
bookmark
dashboard

Wed May 20 2020 19:53:26 GMT+0000 (UTC)

#php
bookmark
dashboard

Mon May 11 2020 14:51:53 GMT+0000 (UTC) https://laravel.com/docs/7.x/database

#php
bookmark
dashboard

Fri Mar 06 2020 19:34:15 GMT+0000 (UTC)

#php
bookmark
dashboard

Fri Mar 06 2020 19:31:17 GMT+0000 (UTC)

#php
bookmark
dashboard

Mon Feb 17 2020 07:36:12 GMT+0000 (UTC)

#android #php
bookmark
dashboard

Thu Feb 06 2020 12:35:11 GMT+0000 (UTC) https://stackoverflow.com/questions/42108036/how-to-disable-cash-on-delivery-on-some-specific-products-in-woocommerce

#php
bookmark
dashboard

Sat Jan 04 2020 18:42:11 GMT+0000 (UTC) https://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php

#php #webdev #howto
bookmark
dashboard

Thu Dec 26 2019 15:18:45 GMT+0000 (UTC) https://www.geeksforgeeks.org/program-count-numbers-fingers/

#php #interesting #interviewquestions #logic
bookmark
dashboard

Wed Dec 25 2019 13:48:42 GMT+0000 (UTC) https://www.geeksforgeeks.org/find-nth-magic-number/

#php #interviewquestions #makethisbetter
bookmark
dashboard

https://stackoverflow.com/a/10368236/7286945

#php

.

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension