我希望能够在注释字段中获取用户输入的文本并检查URL类型表达式,如果存在,则在显示注释时添加锚标记(到url).
我在服务器端使用PHP,在客户端使用Javascript(使用jQuery),所以我应该等到它显示之前检查URL吗?或者在将锚标记插入数据库之前添加它?
所以
<textarea id="comment">check out blahblah.com or www.thisthing.co.uk or http://checkthis.us/</textarea>
Run Code Online (Sandbox Code Playgroud)
变
<div id="commentDisplay">check out <a href="blahblah.com">blahblah.com</a> or <a href="www.thisthing.co.uk">www.thisthing.co.uk</a> or <a href="http://checkthis.us/">http://checkthis.us/</a></div>
Run Code Online (Sandbox Code Playgroud) 我在PHP中使用以下正则表达式从字符串中获取URL
regex = '/https?\:\/\/[^\" ]+/i';
$string = "lorem ipsum http://google.com lorem ipusm dolor http://yahoo.com/something";
preg_match_all($regex, $string, $matches);
$urls = ($matches[0]);
Run Code Online (Sandbox Code Playgroud)
$urls返回所有网址.如何只返回第一个URL?在这种情况下http://google.com.
我试图在不使用foreach循环的情况下实现此目的.