Check if file is an image in php

We can use the php’s inbuilt function getimagesize to get the informations about an file, including its type. While checking for the type we can can also check the file is image or not. So by using getimagesize we can check if file is an image in php

Here is the code to do so

function is_image($path)
{
    $a = getimagesize($path);
    $image_type = $a[2];
     
    if(in_array($image_type , array(IMAGETYPE_GIF , IMAGETYPE_JPEG ,IMAGETYPE_PNG , IMAGETYPE_BMP)))
    {
        return true;
    }
    return false;
}

Pass the file to the function is_image()
$a[0] and $a[1] are the width and height of the image.
$a[2] has the image type.

Hope this helps to check if file is an image in php

US and Canada postal code validation RegEx

When dealing with magento or any other ecommerce websites which is meant to be targeted for specific countries or not we must check validity of the postal code
On one of my recent projects I come with the same problem here I need to validate the postal code for US and Canada
and I found some code snippets to to do so and sharing it on the title US and Canada postal code validation RegEx

^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$

Matches Canadian PostalCode formats with or without spaces (e.g., “T2X 1V4” or “T2X1V4”)

^\d{5}(-\d{4})?$

Matches all US format zip code formats (e.g., “94105-0011” or “94105”)

(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)

Matches US or Canadian zip codes in above formats.

Hop this will help someone for US and Canada postal code validation RegEx

Get countryID from selected / entered shipping address magento

Some times we need to get countryid from selected/entered shipping address, As the checkout page is completely driven by ajax we need to utilize any page which is refreshed like the available shipping methods block
so when ever there is an change in the shipping address it gets reflected on the refreshed shipping method block

The location of the file will be

app/design/{base}/{default}/template/checkout/onepage/shipping_method.phtml

And the code to get the countryID is

Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getData('country_id');

Remove a parameter from url

In this post we are discussing about the ways in which we can remove a parameter from url
we can do it by using preg_replace
and regular expressions

The regular expression used for this is

/\bp\=[^&]+&?/

and the whole code will look like this

preg_replace('/\bp\=[^&]+&?/', "", $string)

Where string is the URL from which the parameter needs to be removed

See the examples

$str[] = 'p=123';
$str[] = 'p=123&bar=456';
$str[] = 'bar=456&p=123';
$str[] = 'abc=789&p=123&bar=456';

foreach ($str as $string) {
	echo preg_replace('/\bp\=[^&]+&?/', "", $string), "<br>";
}

and its out put will be

bar=456
bar=456&
abc=789&bar=456

Hope this gives you and idea about how to remove a parameter from url