Reputation: 754
I have listing all the website address to my overview page. Before that I have to validate the address with the all possible cases.
After several research I found the below regex. But this is not given an exact result.
/((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z0-9\.\/\?\:@\-_=#])*/
My possible test cases are:
'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">'
I want only the domain name. Here I want first five result as true and remain should be false.
Upvotes: 1
Views: 61
Reputation: 1680
Try this:
<?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 />';
}
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
( 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)
)
Upvotes: 1