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

.{4} 
or ....

2. Match first 4 characters

^.{4}

3. Match last 3 characters

.{3}$ 
or
 ...$

4. Match a digit

[0-9]

5. Match an alphabet

[a-zA-Z]

6. Match a digit occurring 0 or more times

[0-9]*

7. Match a lowercase alphabet occurring 1 or more times

[a-z]+

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

[A-Z]?

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
https://www.ietf.org/rfc/rfc0822.txt
, an email address can have letters like { } # $ @ \ / – _ . alphabets numbers and more.

So, a quick regex for matching emails can be

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

this will match {^}#@technacy.net 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)

/^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/iD

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
http://php.net/manual/en/filter.filters.validate.php

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

^(http|https):\/\/((([a-z0-9.-]+\.)+[a-z]{2,4})|([0-9\.]{1,4}){4})(\/([a-zA-Z?-?0-9-_\?\:%\.\?\!\=\+\&\/\#\~\;\,\@]+)?)?$

A detailed and more efficient regex:

+// _^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!(?: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
https://gist.github.com/dperini/729294

However PHP’s inbuilt filter can also be used as the most ideal
use FILTER_VALIDATE_URL as we used FILTER_VALIDATE_EMAIL earlier

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}$

https://regex101.com/r/kA6oD0/6

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));

Atomas Android Game Hack Cheat and Titanium Backup Saved game patch

In this article, I am going to discuss how to cheat the game Atomas – available at Google Play store for free.

What is Atomas?

Well, Atomas is a fascinating puzzle game, which you can learn in seconds but will entertain you for weeks. The perfect game for your spare time!

Your little universe starts with hydrogen atoms only but with the help of the energy rich plus atoms you are able to fusion two hydrogen atoms into one helium atom, 2 helium atoms into one lithium atom and so on. Your primary goal is to create the valuable elements like Gold, Platinum and Silver.

Download it from google android app store Download atomas game.

Requirements

  • Rooted Android Phone (full root access)
  • Any file manager capable of editing xml file (ES File explorer)
  • Titanium Backup (optional)
  • Patch file (edited xml data)

Procedure

  1. Close the running instance of the Game(if any) and also remove it from recent apps
  2. Open your file manager/explorer and move to /data/data/com.sirnic.atomas/shared_prefs
  3. You must find a file named Cocos2dxPrefsFile.xml in this folder. This file is in xml format and hence stores all the info of the current state of game.
  4. Replace this file with the file mentioned in the Requirements section. Or you can alternatively click here to download the file Atomas Game – Cocos2dxPrefsFile.xml
  5. Run the Game and Enjoy 😉

 

Shell script for instant black mass atom (-6) to add any two atoms

busybox killall com.sirnic.atomas
orgusermal=`ls -ln /data/data/com.sirnic.atomas/shared_prefs/Cocos2dxPrefsFile.xml | busybox awk '{print $3}'`
busybox sed -i 's:::' /data/data/com.sirnic.atomas/shared_prefs/Cocos2dxPrefsFile.xml
chown $orgusermal.$orgusermal /data/data/com.sirnic.atomas/shared_prefs/Cocos2dxPrefsFile.xml

How it works? A question for curious ones

This is a tricky part on the name of XML. Since the data is stored in key and data format, it was not too tough to observe which entry stores what information for the game Atomas.

Look at this file:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
 <int name="classic_ufg_b8" value="-10" />
 <int name="classic_ufg_b9" value="-10" />
 <int name="classic_ufg_b4" value="-10" />
 <int name="classic_ufg_b5" value="-10" />
 <int name="classic_ufg_b6" value="-10" />
 <int name="classic_ufg_b7" value="-10" />
 <int name="classic_ufg_b0" value="129" />
 <int name="classic_ufg_b1" value="129" />
 <int name="classic_ufg_b2" value="-10" />
 <int name="classic_ufg_b3" value="-10" />
 <int name="classic_ufg_nu" value="27597" />
 <int name="timeStamp3" value="0" />
 <string name="lastName">Technacy</string>
 <int name="classic_ufg_s" value="129766" />
 <int name="supernovas" value="339" />
 <int name="classic_ufg" value="1" />
 <boolean name="hide_ads" value="true" />
 <int name="luckyCharm" value="7" />
 <int name="neutrinoIntro" value="1" />
 <int name="gamesPlayed2" value="4" />
 <int name="classic_ufg_lv" value="105" />
 <int name="soundOn" value="1" />
 <int name="classic_ufg_hv" value="114" />
 <int name="classic_ufg_mc" value="3141" />
 <int name="localscore_5" value="0" />
 <int name="classic_highelement" value="129" />
 <int name="localscore_4" value="0" />
 <int name="isFirstStart" value="1" />
 <int name="localscore_7" value="0" />
 <int name="localscore_6" value="0" />
 <int name="localscore_1" value="1391" />
 <int name="localscore_0" value="4454" />
 <int name="localscore_3" value="297" />
 <int name="classic_ufg_mwp" value="0" />
 <int name="localscore_2" value="1335" />
 <int name="classic_ufg_b19" value="-10" />
 <int name="classic_ufg_b16" value="-10" />
 <int name="classic_ufg_b15" value="-10" />
 <int name="classic_ufg_b18" value="-10" />
 <int name="classic_ufg_b17" value="-10" />
 <int name="classic_highscore" value="78454" />
 <int name="classic_ufg_b11" value="-10" />
 <int name="wasRated1" value="1" />
 <int name="classic_ufg_b12" value="-10" />
 <int name="classic_ufg_b13" value="-10" />
 <int name="classic_ufg_b14" value="-10" />
 <int name="classic_ufg_cb" value="-1" />
 <int name="classic_ufg_b10" value="-10" />
 <int name="classic_ufg_lc" value="1" />
 <int name="classic_ufg_rnu" value="0" />
 <string name="localname_9"></string>
 <boolean name="gameCenterAndroidLogIn" value="true" />
 <string name="localname_6"></string>
 <int name="classic_ufg_b24" value="-10" />
 <string name="localname_5"></string>
 <string name="localname_8"></string>
 <int name="classic_ufg_b22" value="-10" />
 <string name="localname_7"></string>
 <int name="classic_ufg_b23" value="-10" />
 <string name="localname_2">Malpani</string>
 <int name="classic_ufg_b20" value="-10" />
 <string name="localname_1">Harsh</string>
 <int name="classic_ufg_b21" value="-10" />
 <string name="localname_4"></string>
 <string name="localname_3">Technacy</string>
 <string name="localname_0">Technacy</string>
 <int name="localscore_8" value="0" />
 <int name="localscore_9" value="0" />
</map>

Analysis:

  • The value classic_ufg_cb stores the current atom details.
    • Value 0 is for Hydrogen and thus increasing numbers for higher atoms. ex 7 for oxygen and 5 for carbon
    • -1 means Plus atom
    • -2 means minus
    • -4 means anti matter
    • -5 means copy atom
    • -6 means black plus
  • classic_ufg_s stores current score
  • classic_ufg_lv and classic_ufg_hv store range of atoms which come randomly after every turn (Low and High value)
  • classic_highelement stores the highest atoms discovered in the game.
  • More things can be deduced as the names are quite self-explanatory