Kategori
PHP

Regular Expression ( Regex ) with PHP

Discussion Board : Introduction to the basic concept of Regular Expression syntax in PHP, with the sample few case for using PHP Regex.
Requirement : webserver package, already installed (or just PHP with Apache).

Regex? What it is?
Regular Expression (Regex) is a concept of pattern matching (pattern matching ) within a string. In general, the regular expression used for validation (in an information system development), or used for text processing (text processing). However, sometimes regular expression also often used in Information Retrieval.

Tutorial – tutorial below will mengkover cases – cases that can be handled with Regex, for example by using the Regex pattern matching.

1. What is the pattern “instant noodle” in a string?

[sourcecode language=”php”]
$string = ‘iloveindomieverymuch’;

$isMatch = preg_match("/indomie/", $string); //here the regex

echo $isMatch;
[/sourcecode]

Script above will produce output “1” because the “indomie” pattern exist in the “iloveindomieverymuch” string.diatas akan menghasilkan output “1” karena pattern “indomie” ada dalam string “iloveindomieverymuch”. As we can see, that preg_match will produce boolean value 1 if there is string match, and 0 if not.

2. Apakah string dimulai dengan pattern “ilove” ?
For checking the begin of string, use ^ symbol.

[sourcecode language=”php”]
<?php
$string = ‘iloveindomieverymuch’;
isMatch = preg_match("/^ilove/", $string)

if($isMatch) {
echo ‘The string begins with ilove’; //if there’s a match
}
else {
echo ‘The string does not begin with ilove’; //if there isn’t match
}
?>
[/sourcecode]

This Script above will produce output like below,
The string begins with ilove

3. Does the string end with a pattern “much”?
$ pattern can be used, or perhaps better with as below.

[sourcecode language=”php”]
<?php
$string = ‘iloveindomieverymuchactually’;
isMatch = preg_match("/much\z/i", $string)

if($isMatch) {
echo ‘The string ends with much’; //if there’s a match
}
else {
echo ‘The string does not end with much’; //if there isn’t match
}
?>
[/sourcecode]

When executed, the script above will output as follows,
The string does not end with much

This is because preg_match give value 0, because the string does not meet the rules specified by regex.

4. Strings, including letters or numbers?
The script below will check whether a word including the number (number), or composed of letters (name).

[sourcecode language=”php”]
<?php
$word= ‘ALAN’;
if(preg_match("/^[A-Z]/", $word)) {
$type = ‘name’;
}
else if(preg_match("/^[0-9]/", $word)) {
$type = ‘number’;
}
echo $type;
?>
[/sourcecode]

The output that may result from the script above is as follows,
name

However, if we replace $ word above with the string “1231”, it will produce output,
number

The above tutorial only gives examples of doing a regular expression with preg_match. Of course there are others, in PHP itself to the use of regular expression can be used with functions – functions such as preg_match_all, ereg, eregi, ereg_replace, preg_replace.
… and others.

For thecheatsheet of PHP regular expression, you can go here.

Ok, Happy Coding šŸ˜€

Oleh littleflow3r

obviously, a girl. with sweet smile, off course. turning her 21 years trapped on Informatics departement and just started to having some crush with it lately. she uses wordpress. she loves php. she loves to satisfied her shopping hobbies just via looking around in online shop. oh yeah, she got an A for her english subject^^

1 reply on “Regular Expression ( Regex ) with PHP”

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

This site uses Akismet to reduce spam. Learn how your comment data is processed.