I have the following preg_replace()
function targeting links:
$link = preg_replace(
"#http://([\S]+?)#Uis", '<a href="http://\\1">(link)</a>',
$link
);
It works fine with http
links but obviously not with https
links. How can I adjust it so that it works with https
links as well.
Just add s?
after http
and match the whole link, then use the $0
backreference to refer to it from the replacement pattern:
$link = preg_replace(
"#https?://\S+#i", '<a href="$0">(link)</a>',
$link
);
See the PHP demo
Details :
https?
- either http
or https
://
- a literal char sequence \\S+
- one or more non-whitespace symbols i
- a case insensitive modifier. Note the U
modifier is confusing ( ?
would be written as ??
, and the pattern becomes longer), and I suggest removing it.
The s
modifier does not make sense if the pattern has no .
in it, so it is also redundant, I suggest removing it, too.
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.