Jump to content


Photo

preg_match()


  • Please log in to reply
4 replies to this topic

#1 bilis_money

bilis_money
  • Members
  • PipPipPip
  • Advanced Member
  • 621 posts

Posted 13 June 2006 - 05:31 AM

ok let say i have these codes below;

<?php
// get host name from URL
preg_match("/^(http:\/\/)?([^\/]+)/i",
    "http://www.php.net/index.html", $matches);
$host = $matches[2];

// get last two segments of host name
preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
echo "domain name is: {$matches[0]}\n";
?> 

what bothers me is these --> preg_match("/^(http:\/\/)?([^\/]+)/i",
"http://www.php.net/index.html", $matches);

specially the special characters like back slash, slash, caret, brackets and etc.
i really want to know how to use them properly but unfortunately i tried the manual and
it is incomplete. it doesn't tackle the details specially the special characters with preg_math();

i hope you can give helpful links or advice on where i can study them deeply so that i can understand them.

thanks in advance.


#2 robos99

robos99
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 13 June 2006 - 05:39 AM

Did you try the page in the manual on regex syntax? The page for the preg_match function has very little info on the syntax, but there's a lot of info on the regex syntax page. [a href=\"http://www.php.net/manual/en/reference.pcre.pattern.syntax.php\" target=\"_blank\"]http://www.php.net/manual/en/reference.pcr...tern.syntax.php[/a]

#3 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 13 June 2006 - 05:42 AM

Are you asking what the regular exsperssion format are.
format meaning []^{} whatever

there you go as advised from other member.


Regular Expression Details
Introduction
The syntax and semantics of the regular expressions supported by PCRE are described below. Regular expressions are also described in the Perl documentation and in a number of other books, some of which have copious examples. Jeffrey Friedl's "Mastering Regular Expressions", published by O'Reilly (ISBN 1-56592-257-3), covers them in great detail. The description here is intended as reference documentation. 

A regular expression is a pattern that is matched against a subject string from left to right. Most characters stand for themselves in a pattern, and match the corresponding characters in the subject. As a trivial example, the pattern The quick brown fox matches a portion of a subject string that is identical to itself. 

Meta-characters
The power of regular expressions comes from the ability to include alternatives and repetitions in the pattern. These are encoded in the pattern by the use of meta-characters, which do not stand for themselves but instead are interpreted in some special way. 

There are two different sets of meta-characters: those that are recognized anywhere in the pattern except within square brackets, and those that are recognized in square brackets. Outside square brackets, the meta-characters are as follows: 


\
general escape character with several uses

^
assert start of subject (or line, in multiline mode)

$
assert end of subject (or line, in multiline mode)

.
match any character except newline (by default)

[
start character class definition

]
end character class definition

|
start of alternative branch

(
start subpattern

)
end subpattern

?
extends the meaning of (, also 0 or 1 quantifier, also quantifier minimizer

*
0 or more quantifier

+
1 or more quantifier

{
start min/max quantifier

}
end min/max quantifier

Part of a pattern that is in square brackets is called a "character class". In a character class the only meta-characters are: 

\
general escape character

^
negate the class, but only if the first character

-
indicates character range

]

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#4 bilis_money

bilis_money
  • Members
  • PipPipPip
  • Advanced Member
  • 621 posts

Posted 13 June 2006 - 05:44 AM

thanks

#5 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 13 June 2006 - 03:52 PM

The forward slashes have to be escaped because a forward slash is being used as a delimiter. You could choose a different delimiter and leave the slash as is: '%/%' instead of '/\//'
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users