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?
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]);
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
)
Try this:
preg_match_all("/[a-z]+[:.].*?(?=\s|$)/", 'google.com some text example.com', $matches);
See the result here: http://ideone.com/sBvrSl