如何在 Codeigniter 中链接 URL?
我想在 Codeigniter 中设置一个函数,将 URL 转换为活动链接。我在此处找到了下面的函数——通常适用于 PHP,但是Codeigniter 需要类似的东西。希望在我的整个网站上使用它来发布用户帖子和评论。
$text = preg_replace('/(?<!http:\/\/)(www.[-a-zA-Z0-9@:%_\+.~#?&\/=]+)/i', '<a href="http://\1">\1</a>', $text);
I'd like to setup a function in Codeigniter that would turn URLs into active links. I found the function below here--it's for PHP generally, but need something similar for Codeigniter. Would like to use it throughout my site for user posts and comments.
$text = preg_replace('/(?<!http:\/\/)(www.[-a-zA-Z0-9@:%_\+.~#?&\/=]+)/i', '<a href="http://\1">\1</a>', $text);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我最终使用了 auto_link(),它是 codeigniter 中内置的一个函数。因此,如果有人发布消息并且您想要链接任何网址,只需使用:
我发现它埋在 codeigniter 文档中。
I ended up using auto_link() which is a function built into codeigniter. So if someone posts a message and you want to linkify any urls, simply use:
I found it buried in the codeigniter documentation.
创建一个 helper 并自动加载它。
Make a helper and autoload it.
只需将 preg_replace 命令放入查看您的页面,如下所示:
$ text = "访问网页 http://masalahkita.com 来查看这项工作。Web 正在使用 CodeIgniter 框架并应用此方式。”;
$ link = preg_replace("/([\w]+://[\w-?&;#~=./\@]+[\w/])/i","$1", $text) ;
回显$链接;
Simply put preg_replace command into view your page like this:
$ text = "visit the web http://masalahkita.com to see this work. Web is using CodeIgniter framework and apply this way.";
$ link = preg_replace("/([\w]+://[\w-?&;#~=./\@]+[\w/])/i","$1", $text);
echo $ link;
考虑 CI 中的 URL Helper。有几个函数可以帮助处理 URL。具体看一下
anchor()
函数。Consider URL Helper in CI. There are a couple of functions that assist in working with URLs. Take a look at the
anchor()
function specifically.在
application/helpers
中创建一个名为MY_text_helper.php
的文件,将以下函数放入其中:
现在在您的控制器中:
Create a file called
MY_text_helper.php
inapplication/helpers
put the following function in there:
Now in your controller: