How to modify this regex to include https?
I have this piece of regex that I'm using to create clickable links from URLs entered into a textarea. I did not write the code and am not sure how to modify it so that it will create links if the text s开发者_运维问答tarts with either http or https.
$html = preg_replace('"\b(http://\S+)"', '<a href="$1">$1</a>', stripslashes($rows['body']));
Adding a ?
to the regex makes the preceding character optional.
$html = preg_replace('"\b(https?://\S+)"', '<a href="$1">$1</a>', stripslashes($rows['body']));
Replace
\b(http://\S+)
with:
\b(https?://\S+)
All together:
$html = preg_replace('"\b(https?://\S+)"', '<a href="$1">$1</a>', stripslashes($rows['body']));
No regex expert, so this might work:
$html = preg_replace('"\b(http://\S+|https://\S+)"', '<a href="$1">$1</a>', stripslashes($rows['body']));
精彩评论