Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #16165
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: How i can get preg_match by url? |
| Date | 2016-01-09 10:55 +0100 |
| Organization | PointedEars Software (PES) |
| Message-ID | <5360112.uxWD3WdXnq@PointedEars.de> (permalink) |
| References | <e274c513-b21b-4110-a19a-b45e96327f37@googlegroups.com> <87oacws3f1.fsf@bsb.me.uk> |
Ben Bacarisse wrote:
> thaoluangiavang@gmail.com writes:
>> I want find all urls match with one url. i try code below:
>> <?php
>> $url_regex = '^http://toilatester.com/category/?xv=.*';
>
> You need a delimiter round the pattern.
Not necessarily there, though. The delimiter can also be with the function
call in which $url_regex is used. Also, with this line alone one cannot
know if the final regular expression is constructed by concatenation, or
used in different contexts, in a way that if the delimiter were inserted
here already, it could break the code. So one should be slow to make any
changes in such a line.
> Most often that's / but in this case you'll end up having to quote all
> those /s if you do that.
Note that preg_quote() is _not_ a general solution for this problem as it
would quote all special characters that are part of the PCRE syntax, not
only the specified delimiter. If you only want to quote the delimiter
automatically, you should use preg_replace() on the part of the expression
between the delimiters (str_replace() may not suffice when the expression
uses characters beyond the ISO-8859-1 range).
<http://php.net/preg_quote>
> You can use all sort of characters
The characters that can be used are limited:
$ php -r 'preg_match("fof", "foobar", $matches); print_r($matches);'
PHP Warning: preg_match(): Delimiter must not be alphanumeric or backslash
in Command line code on line 1
PHP Stack trace:
PHP 1. {main}() Command line code:0
PHP 2. preg_match('fof', 'foobar', NULL) Command line code:1
> but I like to use curly brackets.
Using “{” and “}” makes regular expressions harder to read in which
repetition ranges (“a{1, 2}”) are used.
However, I was surprised that not only that a pair of “{” and “}” can be
used in PHP (I had expected that to be Perl-specific), but that apparently
they do not need to be escaped then:
$ php -r 'preg_match("{fo{2}}", "foobar", $matches); print_r($matches);'
Array
(
[0] => foo
)
> Then you need to quote the special character ? so that it means a literal
> ? and not, as yo currently have it, that the / is options.
Probably you mean _optional_.
> Also, you don't need .* at the end.
Depends.
> If the previous part matches it's going to match with .* also, and if the
> previous part does *not* match it won't just because you got .* there:
However, if the expression before “.*” matches, then “.*” would match all
characters until the end of the line (without PCRE_DOTALL) or the text (with
PCRE_DOTALL). Matching does not necessarily mean only testing.
Text: http://toilatester.com/category/?xv=foobarbaz
^-------------------------------------------^
RE: ^http://toilatester.com/category/?xv=.*
Match: http://toilatester.com/category/?xv=foobarbaz
vs.
Text: http://toilatester.com/category/?xv=foobarbaz
^----------------------------------^
RE: ^http://toilatester.com/category/?xv=
Match: http://toilatester.com/category/?xv=
$ php -r 'preg_match_all("`http://foo.example/.*`",
"http://foo.example/bar\nx\nhttp://foo.example/baz", $matches);
print_r($matches);'
Array
(
[0] => Array
(
[0] => http://foo.example/bar
[1] => http://foo.example/baz
)
)
$ php -r 'preg_match_all("`http://foo.example/`",
"http://foo.example/bar\nx\nhttp://foo.example/baz", $matches);
print_r($matches);'
Array
(
[0] => Array
(
[0] => http://foo.example/
[1] => http://foo.example/
)
)
--
PointedEars
Zend Certified PHP Engineer
<http://www.zend.com/en/yellow-pages/ZEND024953> | Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How i can get preg_match by url? thaoluangiavang@gmail.com - 2016-01-07 20:03 -0800
Re: How i can get preg_match by url? "R.Wieser" <address@not.available> - 2016-01-08 10:15 +0100
Re: How i can get preg_match by url? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-01-08 11:42 +0000
Re: How i can get preg_match by url? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-09 10:55 +0100
Re: How i can get preg_match by url? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-09 11:00 +0100
Re: How i can get preg_match by url? SM Bestvideo <thaoluangiavang@gmail.com> - 2016-01-08 20:46 -0800
Re: How i can get preg_match by url? Luuk <luuk@invalid.lan> - 2016-01-09 11:17 +0100
csiph-web