Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #16160 > unrolled thread

How i can get preg_match by url?

Started bythaoluangiavang@gmail.com
First post2016-01-07 20:03 -0800
Last post2016-01-09 11:17 +0100
Articles 7 — 6 participants

Back to article view | Back to comp.lang.php


Contents

  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

#16160 — How i can get preg_match by url?

Fromthaoluangiavang@gmail.com
Date2016-01-07 20:03 -0800
SubjectHow i can get preg_match by url?
Message-ID<e274c513-b21b-4110-a19a-b45e96327f37@googlegroups.com>
I want find all urls match with one url. i try code below:
<?php
$url_regex = '^http://toilatester.com/category/?xv=.*';

$urls = array(
    'http://toilatester.com/category/?xv=123333/time1-one1',
    'http://toilatester.com/category/?xv=78787878/time1-one1',
    'http://toilatester.com/category/?xv=78547547/time1-one1'
);

foreach ($urls as $urllink) {
    if (preg_match($url_regex, $urllink, $a)) {
        $arr_link[] = $a[0];
    }
}
die(print_r($arr_link));
?>
but it not works,
Any idea to help?,
Thank you

[toc] | [next] | [standalone]


#16161

From"R.Wieser" <address@not.available>
Date2016-01-08 10:15 +0100
Message-ID<568f7dd8$0$23728$e4fe514c@news.xs4all.nl>
In reply to#16160
thaoluangiavang (?),

> I want find all urls match with one url. i try code below:
[snip]
> but it not works,
> Any idea to help?,

The pattern string (in $url_regex) should be started and ended by a, choosen
by you, delimiter character.   In your case you could use a hash symbol
character:

$url_regex = '#^http://toilatester.com/category/?xv=.*#';

Than I would suggest to add a switch to indicate that the matching should be
done case insensitive (no difference between upper and lower case
characters).  For that just append an "i" to the end of the pattern string:

$url_regex = '#^http://toilatester.com/category/?xv=.*#i';

Hope that helps,
Rudy Wieser


-- Origional message:
<thaoluangiavang@gmail.com> schreef in berichtnieuws
e274c513-b21b-4110-a19a-b45e96327f37@googlegroups.com...
> I want find all urls match with one url. i try code below:
> <?php
> $url_regex = '^http://toilatester.com/category/?xv=.*';
>
> $urls = array(
>     'http://toilatester.com/category/?xv=123333/time1-one1',
>     'http://toilatester.com/category/?xv=78787878/time1-one1',
>     'http://toilatester.com/category/?xv=78547547/time1-one1'
> );
>
> foreach ($urls as $urllink) {
>     if (preg_match($url_regex, $urllink, $a)) {
>         $arr_link[] = $a[0];
>     }
> }
> die(print_r($arr_link));
> ?>
> but it not works,
> Any idea to help?,
> Thank you

[toc] | [prev] | [next] | [standalone]


#16163

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-01-08 11:42 +0000
Message-ID<87oacws3f1.fsf@bsb.me.uk>
In reply to#16160
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.  Most often that's / but in this
case you'll end up having to quote all those /s if you do that.  You can
use all sort of characters but I like to use curly brackets.  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.  Also, you don't
need .* at the end.  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:

  $url_regex = '{^http://toilatester.com/category/\?xv=}';

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#16165

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2016-01-09 10:55 +0100
Message-ID<5360112.uxWD3WdXnq@PointedEars.de>
In reply to#16163
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.

[toc] | [prev] | [next] | [standalone]


#16166

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2016-01-09 11:00 +0100
Message-ID<1671039.P7f93VaXz6@PointedEars.de>
In reply to#16165
Thomas 'PointedEars' Lahn wrote:

>   Text:   http://toilatester.com/category/?xv=foobarbaz
>           ^-------------------------------------------^
>   RE:    ^http://toilatester.com/category/?xv=.*

    PCRE:  ^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=

    PCRE:  ^http://toilatester.com/category/\?xv=

>   Match:  http://toilatester.com/category/?xv=

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

[toc] | [prev] | [next] | [standalone]


#16164

FromSM Bestvideo <thaoluangiavang@gmail.com>
Date2016-01-08 20:46 -0800
Message-ID<06b4946c-92e3-4f03-882c-4d420079cb1b@googlegroups.com>
In reply to#16160
Vào 11:03:59 UTC+7 Thứ Sáu, ngày 08 tháng 1 năm 2016, SM Bestvideo đã viết:
> I want find all urls match with one url. i try code below:
> <?php
> $url_regex = '^http://toilatester.com/category/?xv=.*';
> 
> $urls = array(
>     'http://toilatester.com/category/?xv=123333/time1-one1',
>     'http://toilatester.com/category/?xv=78787878/time1-one1',
>     'http://toilatester.com/category/?xv=78547547/time1-one1'
> );
> 
> foreach ($urls as $urllink) {
>     if (preg_match($url_regex, $urllink, $a)) {
>         $arr_link[] = $a[0];
>     }
> }
> die(print_r($arr_link));
> ?>
> but it not works,
> Any idea to help?,
> Thank you


all solution to work ok. 
I use this code
$url_regex = '/^http://toilatester.com/category/\?xv=/'; 

Many thanks.

[toc] | [prev] | [next] | [standalone]


#16167

FromLuuk <luuk@invalid.lan>
Date2016-01-09 11:17 +0100
Message-ID<5690de55$0$23838$e4fe514c@news.xs4all.nl>
In reply to#16164
On 09-01-16 05:46, SM Bestvideo wrote:
> Vào 11:03:59 UTC+7 Thứ Sáu, ngày 08 tháng 1 năm 2016, SM Bestvideo đã viết:
>> I want find all urls match with one url. i try code below:
>> <?php
>> $url_regex = '^http://toilatester.com/category/?xv=.*';
>>
>> $urls = array(
>>      'http://toilatester.com/category/?xv=123333/time1-one1',
>>      'http://toilatester.com/category/?xv=78787878/time1-one1',
>>      'http://toilatester.com/category/?xv=78547547/time1-one1'
>> );
>>
>> foreach ($urls as $urllink) {
>>      if (preg_match($url_regex, $urllink, $a)) {
>>          $arr_link[] = $a[0];
>>      }
>> }
>> die(print_r($arr_link));
>> ?>
>> but it not works,
>> Any idea to help?,
>> Thank you
>
>
> all solution to work ok.
> I use this code
> $url_regex = '/^http://toilatester.com/category/\?xv=/';
>
> Many thanks.
>

This will not work because of the '/' characters in the url_regex....

use a different character (as was suggested by R.Wieser):
$url_regex = '#^http://toilatester.com/category/\?xv=#';

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web