我有一个小问题需要帮助。
如果 $post['text'] 等于这样的数据:“https://stackoverflow.com/blah?blah 是一个很酷的网站”,怎么可能只在类型标签中制作 URL 位...
所以最终的 HTML 结果会是这样的
<p><a href="https://stackoverflow.com/blah?blah">https://stackoverflow.com/blah?blah</a> is such a cool website</p>
我有点菜鸟。我实际上是一个 14 岁的 child ,对编码还是有点陌生,但我喜欢它!
您可以使用正则表达式。
function make_hyperlink($text)
{
// match protocol://address/path/
$text = preg_replace("{[a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*}", "<a href=\"\\0\" target='_blank'>\\0</a>", $text);
// match www.something
$text = preg_replace("{(^| )(www([.]?[a-zA-Z0-9_/-])*)}", "\\1<a href=\"http://\\2\" target='_blank'>\\2</a>", $text);
// return $text
return $text;
}
echo make_hyperlink('http://stackoverflow.com/blah?blah');
使用上面的函数。它将用简单的 url 文本替换 URL。