Insert Indian States in Magento Database

Connect to your server’s database using mysql cli or phpmyadmin and use the following query

INSERT INTO `directory_country_region` VALUES 
(NULL,"IN","AP","Andhra Pradesh"),
(NULL,"IN","AR","Arunachal Pradesh"),
(NULL,"IN","HP","Himachal Pradesh"),
(NULL,"IN","JK","Jammu and Kashmir"),
(NULL,"IN","MP","Madhya Pradesh"),
(NULL,"IN","TN","Tamil Nadu"),
(NULL,"IN","UP","Uttar Pradesh"),
(NULL,"IN","WB","West Bengal"),
(NULL,"IN","AN","Andaman and Nicobar Islands"),
(NULL,"IN","DH","Dadra and Nagar Haveli"),
(NULL,"IN","DD","Daman and Diu"),

How to create Magento 2 category programmatically?

Use this code:

use \Magento\Framework\App\Bootstrap;
echo 'code by harshvardhanmalpani';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

function createCategory($a='',$b=2,$c=true,$d='',$e='',$f='',$g='') {
        global $objectManager;
        $category = $objectManager->get('\Magento\Catalog\Model\CategoryFactory')->create();
        $category->setParentId($b); // 1: root category.
'description' => $d,
'meta_title' => $e,
'meta_keywords' => $f,
 'meta_description' => $g,
createCategory("Xyz",4,false,"description","m title");

How to secure a folder by whitelisting one IP using HTACCESS and denying all others

The process involves matching all requests for an IP, if the IP does not match redirect all secured directory requests.

RewriteCond %{REMOTE_ADDR} !^120\.120\.120\.120
RewriteRule ^admin/.* - [L,R=403] 
RewriteCond %{REMOTE_ADDR} ^120\.120\.120\.120
RewriteRule ^admin$ - [L,R=403]

If you want to control this IP using a script, you can add 2 lines

# your rules will appear here using php script

Now add a script, say happy.php
and use the following code:

$htaccess = file_get_contents('.htaccess');
$ip= str_replace(".","\.",$_SERVER['REMOTE_ADDR']);
$rules="RewriteCond %{REMOTE_ADDR} !^".$ip."
RewriteRule ^admin/.* - [L,R=403]
RewriteCond %{REMOTE_ADDR} !^".$ip."
RewriteRule ^admin$ - [L,R=403]";
$problem="###CUSTOM RULES###\r\n".$rules.
"\r\n###CUSTOM RULES###";
#please copy the next line carefully, no extra spaces or new lines
$solution=preg_replace('/###CUSTOM RULES###.*?###CUSTOM RULES###/sm',$problem,$htaccess);
file_put_contents('.htaccess', $solution);

you can also specify your error page for specific http code: 403
ErrorDocument 403 /er.htm

Start a magento instance programmatically

This Code:

$mageFilename = 'app/Mage.php';
require_once $mageFilename;
ini_set('display_errors', 1);
error_reporting(E_ALL &amp; ~E_NOTICE);
Mage::register('isSecureArea', 1);
//your mage code here

Commonly used regular expressions (regex) for developers in PHP

Commonly used regular expressions (regex) for developers in PHP

Regex are used or can be used almost everywhere strings are involved. If you are dealing with text strings, you would often find patterns in them and you might be required to filter few of them based on some similarities and properties. For example: a currency or money is often written in this form –
[Currency symbol] [Digits] ([.] [Digits])maybe

Now, in order to filter any such things programmatically using a script, few of really helpful regular expressions are mentioned below:

1. Match 4 characters

or ....

2. Match first 4 characters


3. Match last 3 characters


4. Match a digit


5. Match an alphabet


6. Match a digit occurring 0 or more times


7. Match a lowercase alphabet occurring 1 or more times


8. Match an uppercase alphabet either NOT occurring or occurring once ( 0 or 1 time)


9. Match a character occurring exactly once
write the character itself in regex; similar to #4 and #5

10. Match a character literally (used to match meta-characters)
use \ before the character to be matched
If you want to find the occurrence of question mark(s) in a string
use \? (remember ? is used to find occurrence of at most 1 time but \? with find occurrence of ? itself exactly once)

Pro things

Match an e-Mail address
a regular expression to match an email address with 100% efficiency is really tough to be learnt here because according to RFC 0822
, an email address can have letters like { } # $ @ \ / – _ . alphabets numbers and more.

So, a quick regex for matching emails can be

^[a-zA-Z0-9\._%\-^#\{\}][email protected]([a-zA-Z0-9.-]+\.)+[a-zA-Z]{2,9}$

this will match {^} which is an actual email address and is totally valid.

However this won’t work all the times, a more detailed regex would be something like this: (email addresses can end with IP addresses too)


So, what should be the ideal solution because you won’t like to check this^ regex all the time
Use PHP’s inbuilt filter – FILTER_VALIDATE_EMAIL

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {   $emailErr = "Invalid email format";  }

For security, w3schools mentioned passing and sanitizing all the data first with a function:

function test_input($data) {   $data = trim($data);   $data = stripslashes($data);   $data = htmlspecialchars($data);   return $data; }

Matching a URL
A URL can have letters, digits, any character as a parameter or in host details. A simple version of regex to match http urls is


A detailed and more efficient regex:

+// _^(?:(?:https?|ftp)://)(?:\S+(?::\S*)[email protected])?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]-*)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]-*)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?$_iuS

Diego Perini

However PHP’s inbuilt filter can also be used as the most ideal

Match a Phone number
Regex for a phone number is easy if you want to match only MOBILE NUMBERS which must be of 10 digits and can include a country code like +91, 091, (91), (+91) or 0

^(091|\+91|91|\(091\)|\(\+91\)|\(91\)|0)? ?[7-9][0-9]{9}$

Match a Name
A name is supposed to have alphabets only but the length of first names and last names is not fixed or general at all. These lengths may vary thus the regex sounds funny at times.
^[a-zA-Z’ -]+$

expression, match, php, regex

Clean html code with REGEX

$stripped = trim(preg_replace('/\s+/', ' ', $sentence));