Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Find preg match all url with regex url

10 views
Skip to first unread message

Tan Tran

unread,
Jun 10, 2015, 12:41:39 AM6/10/15
to
Hello,
I have issue when use preg match,

$mystring = '<div class="test">
<a href="http://stackoverflow.com/questions/30747165/overriding-function-with-sinon-mock.html">questtion 1</a>
<a href="http://stackoverflow.com/questions/30747165/dshdsjjdsjdjkdsjk.html">questtion 2</a>
<a href="http://stackoverflow.com/questions/30747165/yurererererererere.html">questtion 3</a>
</div>';

$url_regex = 'http://stackoverflow.com/questions/30747165/*.html';

preg_match_all(url_regex, $mystring, $urls);

print_r($urls);
but it don't work. How i can get all url from my string match with $url_regex

thanks.

Thomas Mlynarczyk

unread,
Jun 10, 2015, 4:53:54 AM6/10/15
to
On 10/06/15 06:41, Tan Tran wrote:
> $mystring = '<div class="test">
> <a href="http://stackoverflow.com/questions/30747165/overriding-function-with-sinon-mock.html">questtion 1</a>
> <a href="http://stackoverflow.com/questions/30747165/dshdsjjdsjdjkdsjk.html">questtion 2</a>
> <a href="http://stackoverflow.com/questions/30747165/yurererererererere.html">questtion 3</a>
> </div>';
>
> $url_regex = 'http://stackoverflow.com/questions/30747165/*.html';

Your regex is missing delimiters (I use ~ below). A dot (.) has a
special meaning in a regex ("any character") and must therefore be
escaped (\.) if you want to match *only* a dot. (Doesn't really matter
here though.) "/*" would match zero or more slashes -- not what you want.

$url_regex = '~http://stackoverflow\.com/questions/30747165/.+?\.html~';

> preg_match_all(url_regex, $mystring, $urls);

preg_match_all($url_regex, $mystring, $urls);
---------------^

Greetings,
Thomas

--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
0 new messages