I'm trying to extract URL from a piece of string I have different posts that contains URL in their message. I've prepared a pattern to match but it's not working properly.
Tried Regex
$pattern1= '%\b((https?://)|(www\.)|(^[\D]+\.))[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))%';
$pattern2= '%\b^((https?://)|(www\.)|(^[a-z]+\.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%';
CODE
for ( $i = 0; $i < $resultcount; $i ++ ) {
$pattern = '%\b^((https?://)|(www\.)|(^[a-z]+\.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%';
$message = (string)$result[$i]['message'];
preg_match_all($pattern,$message,$match);
print_r($match);
}
A Example of my post like this
"This is just a post to test regex for extracting URL http://google.com, https://www.youtube.com/watch?v=dlw32af https://instagram.com/oscar/ en.wikipedia.org"
Post may have comma or may not have comma for multiple URLs
Thank you people :)
This should get you started:
\b(?:https?://)?(?:(?i:[a-z]+\.)+)[^\s,]+\b
\b # a word boundary
(?:https?://)? # http:// or https://, optional
(?:(?i:[a-z]+\.)+) # any subdomain before
[^\s,]+ # neither whitespace nor comma
\b # another word boundary
See a demo on regex101.com.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With