Tag Archives: regex

PHP Regex Pattern Test Tool Software & Sites 05 DEC 2014

Annoyingly, my regex, even after all these years of web development, is still completely horrible – pretty much non-existent actually. Which is exactly why I always like to keep a handy regex pattern test tool on hand for the odd occasion when I do actually need to use the power of regular expressions.

Happily, Philip Bjorge saw fit to put together a cool little online PHP Regex test pad in the form of PHP Live Regex.

It’s simple to use, updates its regex results in realtime, and has a whole lot of helper text sitting at the bottom of the screen.

Quite a nice little implementation then in other words.

phpliveregex.com

Related Link: http://www.phpliveregex.com/

Javascript: How to Extract a Number from a String Programming 04 JUL 2013

javascript logoIn my web development work, I’m quite fond of using the trick of generating grouped elements by making sure their IDs all share a common number with a bit of unique text.

So for example I could have a set of elements with the IDs accordionheading1, accordionpanel1, accordiontext1 and accordionheading2, accordionpanel2, accordiontext2. Now in order for this to be useful, I need to be able to extract the number from these strings so that I can automatically guess an element’s associated partners, meaning that you now know exactly what is coming next:

One of the easier ways of extracting a number from a string using javascript is by making use of a bit of regex, utilizing the digit negated shorthand character class \D to do our dirty work.

In practice:

var element = 'accordion24paneldrop';
id = element.replace( /\D+/g, '');
alert(id); //24

Nifty.

Check it else for yourself using JSFiddle! (JS Bin is pretty cool too)

PHP: Simple REGEX Quick Reference for use with preg_match Function CodeUnit 30 NOV 2011

As always, checking to see if one string is contained within another will always be faster using the strpos or strstr functions. However, for more complex checks, REGEX (regular expressions) are the ticket, and with functions like PHP’s preg_match, there really isn’t any excuse for not doing proper value validation.

Anyway, just as a note to myself, here is a quick reference sheet for some basic regex:

...

PHP: Use REGEX to Ensure that your received Variable is a Number CodeUnit 20 MAY 2011

It is a good idea to check that the variables your script is receiving are in fact of the correct type in terms of value passed. For example, when receiving some sort of record ID value, 99% of the time we are expecting a numeric value. A simple method to ensure that the variable to be processed is indeed numeric, is through the use of a simple REGEX statement applied using the preg_replace PHP function.

As an example, let’s process a user ID passed to us via a POST to our script:

$userid = (key_exists('user-id', $_POST)) ? preg_replace("@[^0-9-]@", '', $_POST['user-id']) : '-1';

What the above does is first check that the user-id is indeed present in the $_POST variable. If it isn’t, set our internal variable to -1. If it is present, replace all characters that don’t fall within the set of 0 to 9, excluding dashes, with a blank. This is assigned back to our variable for internal use.

So a simple check to guarantee that we’ve received either a positive (2) or negative (-2) number!

Nifty.

PHP: How to Remove or Replace Strings from the Front and End of a Value CodeUnit 02 JUL 2010

Today’s little PHP hint is a pretty simple one, but one worth tackling if you are looking for a quick and easy way to strip a particular string pattern off the front and end of a value.

What we will make use of is of course PHP’s built in preg_replace functionality, basically one of the wrapper functions that brings regular expression manipulation to PHP.

Now to start off, we need to specify the string pattern we wish to search for off the front of our value. To do this we make use of the ^ regex control character that denotes the search to begin from the start of a string. To locate a string pattern off the end of our value, we make use of the $ regex control character which likewise, tells the search to commence at the back of the given string value.

We then place our two patterns in a search array, before moving on to define our replace array. Now at this stage you can choose: if you want to simply strip these patterns off the value, then simply match the search patterns with the same amount of blank string values, otherwise insert the string values you wish to replace the patterns with.

Finally, we call the preg_replace function, feeding it our search and replace arrays, as well as the input value it is to work on.

In other words, we would have something like this:

$search = array ( "/^stringatstart /","/ stringatend$/");
$replace = array ('','');

echo preg_replace($search, $replace, "stringatstart Hello World! stringatend");

And there you go, a nice and simple way to strip off specific values at either end of a string! :)

PHP: How to Get all the Numbers out of an Alphanumeric String? CodeUnit 07 MAY 2010

Given a registration code, or something important like that, which often contains information encoded in the alphanumeric string itself, it is sometimes quite useful to be able to simply strip out the letters from the string, leaving only the numeric part behind.

So for example, for a code that looks like LTTCRA003 (my old University student number in case you were wondering), it might be of use to pull out the digit part as that information tells you how many times the LTTCRA hash of name and surname has occurred before.

Now in order to grab the numbers we are simply going to knock out all the letters contained in the string and for that we will use preg_replace, a function which makes use of regular expression matching to make its string replacements.

So what exactly do we need to do in terms of code then?

Well, the regular expression itself is pretty simple (probably the most simple you can get), and put into code, this is what will solve our little problem for us:

$numbers = preg_replace('/[a-zA-Z]/','','LTTCRA003');

The result of this code will be ‘003’, thanks to us replacing all the letters with blanks.

And there you go. That’s one way of getting all the numbers out of an alphanumeric string! :)

PHP: How to Get all the Letters out of an Alphanumeric String? CodeUnit 21 APR 2010

Given a registration code, or something important like that, which often contains information encoded in the alphanumeric string itself, it is sometimes quite useful to be able to simply strip out the numbers from the string, leaving only the letters behind.

So for example, for a code that looks like LTTCRA003 (my old University student number in case you were wondering), it might be of use to pull out the letter part as that information tells you what hash of my name and surname computes to.

Now in order to grab the letters we are simply going to knock out all the numbers contained in the string and for that we will use preg_replace, a function which makes use of regular expression matching to make its string replacements.

So what exactly do we need to do in terms of code then?

Well, the regular expression itself is pretty simple (probably the most simple you can get), and put into code, this is what will solve our little problem for us:

$letters = preg_replace('/[0-9]/','','LTTCRA003');

The result of this code will be ‘LTTCRA’, thanks to us replacing all the numbers with blanks.

And there you go. That’s one way of getting all the letters out of an alphanumeric string! :)

Cross Platform Regular Expression (RegEx) Tool: RegExr CodeUnit 11 JAN 2010

Regular expressions are plain and simple an enormous headache – but man when you get them to work for you then they are the most powerful guns known to any string-manipulating madman out there.

By now of course there are plenty of great tools to run and test and flex your regular expression muscle out there, floating around on the Net, but you can now safely add a new cat to the bag with the release of the platform independent RegExr, built on the Adobe Air platform using the wonder that is Flex 3.

Grant Skinner’s little gskinner group have slapped together a great little Adobe Air app that allows you to play around with regular expressions to your heart’s content, but at the same time provides you with some great sample data and tutorial information in order to increase your regex knowledge, as well as a nifty little community feature that allows you to save and share your regex recipes with other like-minded madmen out there.

It’s a fantastic little desktop tool that runs equally well on Ubuntu, Windows or a Mac, and if you’re too lazy to download the desktop application, you can even run it straight from your browser at http://gskinner.com/RegExr/.

To download/install the far more nifty Adobe Air app though, browse through to the mobile ready site at http://www.gskinner.com/RegExr/desktop/

Enjoy.

Related Link: http://www.gskinner.com/RegExr/desktop/