Jump to content


This topic is now archived and is closed to further replies.



Recommended Posts

ok let say i have these codes below;

// get host name from URL
    "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.

Share this post

Link to post
Share on other sites
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]

Share this post

Link to post
Share on other sites
Are you asking what the regular exsperssion format are.
format meaning []^{} whatever

there you go as advised from other member.

Regular Expression Details
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.

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


Share this post

Link to post
Share on other sites
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 '/\//'

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.