I am trying to replace all urls in the text with hyperlink using regular expression. The urls must start with either http://
or https://
. And they must contain some TLD, eg .com
, .org
, or .co.uk
etc.
Below is my regex
pattern in PHP
:
$pattern = "/(http)+(s)?:\/\/(\S)+(\.){1}/i";
So if you use the following code:
$str = "this http://dd is a String http://www.example.com and this a String https://anotherexample.co.uk";
echo preg_replace($pattern, "<a href='$0' target='_blank'>$0</a>", $str);
It gives me following output:
You can see that the TLD part is not included in the hyperlink. So how can I convert capturing group (\.){1}
to non-capturing group to also cover TLD?
You can try this:
$pattern = "/https?:\/\/(\S)+(\.\w{1,4})+/i"
echo preg_replace($pattern, "<a href='$0' target='_blank'>$0</a>", $str);
In the pattern:
https?
: http or https
(\.\w{1,4})+
: TLDs like .co .com or something like .co.uk the maximum length of each TLD is 4 here but you can change that.
Use the following pattern:
https?:\/\/[az]+\.[az]+[.az]* /i
?
[az]+
to capture the first set of letters after 'https'[.az]*
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.