PHP Regular Expressions

edr

Regular expressions are the patterns that allow you to process text based on certain requirement. This in turn produces the sequence or pattern of text that is based on pattern-matching as per developers needs. You can do a lot of things using regular expressions. For example, you can filter our the text in uppercase or replace the word with something else, match the group of text and filter the content. There are plenty of other ways to use the regular expression as per your needs.

PHP allows you to use two types of regular expressions.

  • POSIX Regular Expressions
  • PERL Style Regular Expressions

I suggest you to follow the PERL Compatible Regular expressions as they’re widely used. There isn’t much difference between POSIX and PCRE but you’ve to learn separately to know them better.

There are ways to explain the valid and invalid patterns of regular expressions. I suggest you to use the examples to learn about these valid patterns. This is the reason I am not going to explain you every little expression, Instead you’ll learn from the examples posted here.

Let’s take a look at important perl compatible regular expression functions.

preg_match() : This function performs a regular expression match. Check the following code to see it in action.

<? php
$str="this is demo";
$str1=preg_match("/this/",$str);
$str2=preg_match("/that/",$str);
echo $str1;
echo "<br/>";
echo $str2;
? >

Check the output and you’ll see that it shows 1 and 0. Here value 1 means we have found match and 0 means that there is no match. You can process much more complex data with the help of loops and reg operators.

preg_replace(): This function performs regular expression search and replace operation. See the following code so that you get idea of how preg_replace() works.

<? php
$data="Here is some simple text";
$find="/is/";
$replace="is our";
echo preg_replace($find,$replace,$data);
? >

There are two more optional arguments that you can pass to the preg_replace() to count the replacements and also to restrict the number of replacements of text.

preg_split(): This function takes your input and then split the input into multiple arrays. The value in array is dependent on your operator usage for spliting the text. PREG_SPLIT_NO_EMPTY argument ensures that no empty results are passed to the output of array. Check the following code on preg_split and see how we’re spliting the two statements that are separated with dot.

<? php
$data="Here is some simple text. Run the program";
$prodata=preg_split('/\./',$data,-1,PREG_SPLIT_NO_EMPTY);
print_r($prodata);
? >

Check the output and you’ll see that it splits the sentences in two arrays starting from [0].

This was just short introduction on regular expressions in php. You can read more about regular expressions in general from wikipedia. If you’re python programmer then do read python regular expressions tutorial.

Feel free to let me know your suggestions and feedback on this tutorial. I would love to improve if there are any constructive criticisms in the comments.

abu murad

Abu Murad , is fascinated by the constantly changing, complex, layered world of SEO, & content marketing’s ability to reach potential buyers on their terms, in their timing, and in the ways that are most relevant to them.

Leave a Comment