Jump to content

preg_match()


bilis_money

Recommended Posts

ok let say i have these codes below;

[code]
<?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";
?>
[/code]

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.
Link to comment
https://forums.phpfreaks.com/topic/11838-preg_match/
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]
Link to comment
https://forums.phpfreaks.com/topic/11838-preg_match/#findComment-44862
Share on other sites

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

there you go as advised from other member.


[code]
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

]
[/code]
Link to comment
https://forums.phpfreaks.com/topic/11838-preg_match/#findComment-44863
Share on other sites

Archived

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

×
×
  • Create New...

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.