简体   繁体   中英

preg_match pattern-matching expression to get URL in php

I have a text as follows :
I have this text 'The text (is in parenthesis) ends'
here: www.url1.com
and here: some text (www.url2.com)

I want to extract www.url2.com and store in a variable.
I am using the pattern : preg_match('/(?<=()(.+)(?=))/is', $message, $match);
This fails cause of the parenthesis in line 1.
The string extracted is : is in parenthesis)
Please help me by providing a suitable regex expression.

if (preg_match('/\(\s?(www\.\w+\.com)\s?\)/', $text, $matches)){
    $var = $matches[1];
}

That assumes .com . If you want to allow, for example, .net and .org , you can modify it as:

if (preg_match('/\(\s?(www\.\w+\.(com|net|org))\s?\)/', $text, $matches)){
    $var = $matches[1];
}
preg_match("#\((www\.[^\s]+)\)#", $text, $match);

echo $match[1];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM