3

Ok, I'm trying to get hostnames and i'm using this regex:

preg_match_all("/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/", 'google.com some text example.com', $matches);
print_r($matches[0]);

Matches should be:

google.com
example.com

but, the output is the first match only (google.com)

What i should change to get all matches please?

Community
  • 1
  • 1
user2203703
  • 1,955
  • 4
  • 22
  • 36
  • The regular expression that you're using is to test whether a string is a hostname; it's not meant to match *all* possible hostnames it can find inside a string. – Ja͢ck Mar 05 '15 at 08:19

3 Answers3

1

One cheap trick you could apply is to simply replace your anchors with a word boundary assertion:

preg_match_all("/\b(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])\b/", 'google.com some text example.com', $matches);
//               ^^                                                                                                         ^^

That would yield:

Array(
  [0] => google.com
  [1] => some
  [2] => text
  [3] => example.com
)

To filter out word without at least one period:

preg_match_all("/\b(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)+([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])\b/", 'google.com some text example.com', $matches);
//                                                                       ^
print_r($matches[0]);
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

It's actually return an empty array for me because you have start of line (^) and end of line ($) in your pattern. If you remove this you will get more results. But still not google.com and exaple.com because you RegExp is written that way that 1 letter is enough. That what I got

Array
(
    [0] => google.c
    [1] => o
    [2] => m
    [3] => s
    [4] => o
    [5] => m
    [6] => e
    [7] => t
    [8] => e
    [9] => x
    [10] => t
    [11] => example.c
    [12] => o
    [13] => m
)
SeriousDron
  • 1,296
  • 9
  • 12
0

Try this:

preg_match_all("/[a-z]+[:.].*?(?=\s|$)/", 'google.com some text example.com', $matches);

See the result here: http://ideone.com/sBvrSl

kitensei
  • 2,510
  • 2
  • 42
  • 68