Try this:
Code:
<?php
$input = 'test.com
http://www.google.com
www.google.com
https://google.com
https://www.google.com
testetst
img src="/test/test" >
<img src="/test/test.png" alt="page" title="page">';
echo '<h3>Input</h3><pre>'.htmlentities($input).'</pre><h3>Output</h3>';
preg_match_all('%(http[s]{0,1}://)*([A-Za-z0-9-]*?\.){0,1}([A-Za-z0-9-]*?\.[A-Za-z0-9-]*?)[\s]*(\r\n|\n\r|\r|\n|$)%', $input, $regs, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($regs[0]); $i++) {
// $regs[3][$i] contains domain name
echo $regs[3][$i] . '<br />';
}
Result:
Input:
test.com
http://www.google.com
www.google.com
https://google.com
https://www.google.com
testetst
img src="/test/test" >
<img src="/test/test.png" alt="page" title="page">
Output:
test.com
google.com
google.com
google.com
google.com
The Regex in detail:
( Match the regular expression below and capture its match into backreference number 1
http Match the characters “http” literally
[s] Match the character “s”
{0,1} Between zero and one times, as many times as possible, giving back as needed (greedy)
:// Match the characters “://” literally
)* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
( Match the regular expression below and capture its match into backreference number 2
[A-Za-z0-9-] Match a single character present in the list below
A character in the range between “A” and “Z”
A character in the range between “a” and “z”
A character in the range between “0” and “9”
The character “-”
*? Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
\. Match the character “.” literally
){0,1} Between zero and one times, as many times as possible, giving back as needed (greedy)
( Match the regular expression below and capture its match into backreference number 3
[A-Za-z0-9-] Match a single character present in the list below
A character in the range between “A” and “Z”
A character in the range between “a” and “z”
A character in the range between “0” and “9”
The character “-”
*? Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
\. Match the character “.” literally
[A-Za-z0-9-] Match a single character present in the list below
A character in the range between “A” and “Z”
A character in the range between “a” and “z”
A character in the range between “0” and “9”
The character “-”
*? Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
)
[\s] Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
( Match the regular expression below and capture its match into backreference number 4
Match either the regular expression below (attempting the next alternative only if this one fails)
\\r Match a carriage return character
\\n Match a line feed character
| Or match regular expression number 2 below (attempting the next alternative only if this one fails)
\\n Match a line feed character
\\r Match a carriage return character
| Or match regular expression number 3 below (attempting the next alternative only if this one fails)
\\r Match a carriage return character
| Or match regular expression number 4 below (the entire group fails if this one fails to match)
\\n Match a line feed character
| Or match regular expression number 5 below (the entire group fails if this one fails to match)
\$ Assert position at the end of the string (or before the line break at the end of the string, if any)
)