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?

$string = 'iloveindomieverymuch';

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

echo $isMatch;

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.

$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

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.

$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

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

$word= 'ALAN';
if(preg_match("/^[A-Z]/", $word)) {
$type = 'name';
else if(preg_match("/^[0-9]/", $word)) {
$type = 'number';
echo $type;

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

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

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 :D
Hal yang juga menarik:

Hak Cipta

Semua skrip dan teknik dalam artikel di itx.web.id boleh digunakan sebagaimana kehendakmu tanpa perlu mencantumkan sumber. Kamu tidak boleh mengkopi seluruh artikel, dalam Bahasa Indonesia ataupun diterjemahkan ke dalam bahasa lain.

Related Articles:

Artikel terkait:


About the author

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 lo

1 Comment

Tinggalkan Balasan

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

x( X-( B-) ;-) :wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :D :?: :-| :-x :-o :-P :-D :-? :-)) :-) :-( :-& :) :( :!: 8-O 8-) 8) (Русский) (yahoo) (worship) (woot) (wave) (unsure) (tongue) (thinking) (tears) (taser) (smileydance) (sleeping) (sick) (scenic) (rofl) (rock) (party) (panic) (okok) (nottalking) (ninja) (music) (muscle) (muhaha) (money) (mmm) (lonely) (lol) (lmao) (idiot) (hungry) (highfive) (heart_beat) (heart) (headspin) (hassle) (haha) (gym) (griltongue) (goodluck) (girlkiss) (funkydance) (fish_hit) (eyeroll) (evilsmirk) (evil_grin) (drinking) (doh) (devil) (dance) (cry) (cozy) (coffee) (brokenheart) (bringit) (blush) (bigeyes) (beer) (banana_rock) (banana_ninja) (banana_cool) (applause) (annoyed) (angry) (K) (: