<tfoot id='rZ6i4'></tfoot>

<i id='rZ6i4'><tr id='rZ6i4'><dt id='rZ6i4'><q id='rZ6i4'><span id='rZ6i4'><b id='rZ6i4'><form id='rZ6i4'><ins id='rZ6i4'></ins><ul id='rZ6i4'></ul><sub id='rZ6i4'></sub></form><legend id='rZ6i4'></legend><bdo id='rZ6i4'><pre id='rZ6i4'><center id='rZ6i4'></center></pre></bdo></b><th id='rZ6i4'></th></span></q></dt></tr></i><div id='rZ6i4'><tfoot id='rZ6i4'></tfoot><dl id='rZ6i4'><fieldset id='rZ6i4'></fieldset></dl></div>

    1. <small id='rZ6i4'></small><noframes id='rZ6i4'>

      <legend id='rZ6i4'><style id='rZ6i4'><dir id='rZ6i4'><q id='rZ6i4'></q></dir></style></legend>

          <bdo id='rZ6i4'></bdo><ul id='rZ6i4'></ul>
      1. 在 PHP 中将纯文本 URL 转换为 HTML 超链接

        Convert plain text URLs into HTML hyperlinks in PHP(在 PHP 中将纯文本 URL 转换为 HTML 超链接)
        1. <i id='DDtNq'><tr id='DDtNq'><dt id='DDtNq'><q id='DDtNq'><span id='DDtNq'><b id='DDtNq'><form id='DDtNq'><ins id='DDtNq'></ins><ul id='DDtNq'></ul><sub id='DDtNq'></sub></form><legend id='DDtNq'></legend><bdo id='DDtNq'><pre id='DDtNq'><center id='DDtNq'></center></pre></bdo></b><th id='DDtNq'></th></span></q></dt></tr></i><div id='DDtNq'><tfoot id='DDtNq'></tfoot><dl id='DDtNq'><fieldset id='DDtNq'></fieldset></dl></div>

              <legend id='DDtNq'><style id='DDtNq'><dir id='DDtNq'><q id='DDtNq'></q></dir></style></legend>
                <tbody id='DDtNq'></tbody>

              <small id='DDtNq'></small><noframes id='DDtNq'>

              <tfoot id='DDtNq'></tfoot>
                • <bdo id='DDtNq'></bdo><ul id='DDtNq'></ul>
                  本文介绍了在 PHP 中将纯文本 URL 转换为 HTML 超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个简单的评论系统,人们可以在其中提交纯文本字段内的超链接.当我将这些记录从数据库返回到网页中时,我可以使用 PHP 中的什么 RegExp 将这些链接转换为 HTML 类型的锚链接?

                  I have a simple commenting system where people can submit hyperlinks inside the plain text field. When I display these records back from the database and into the web page, what RegExp in PHP can I use to convert these links into HTML-type anchor links?

                  我不希望算法使用任何其他类型的链接执行此操作,仅使用 http 和 https.

                  I don't want the algorithm to do this with any other kind of link, just http and https.

                  推荐答案

                  这是另一个解决方案,这将捕获所有 http/https/www 并转换为可点击的链接.

                  Here is another solution, This will catch all http/https/www and convert to clickable links.

                  $url = '~(?:(https?)://([^s<]+)|(www.[^s<]+?.[^s<]+))(?<![.,:])~i'; 
                  $string = preg_replace($url, '<a href="$0" target="_blank" title="$0">$0</a>', $string);
                  echo $string;
                  

                  或者只是捕获 http/https,然后使用下面的代码.

                  Alternatively for just catching http/https then use the code below.

                  $url = '/(http|https|ftp|ftps)://[a-zA-Z0-9-.]+.[a-zA-Z]{2,3}(/S*)?/';   
                  $string= preg_replace($url, '<a href="$0" target="_blank" title="$0">$0</a>', $string);
                  echo $string;
                  

                  下面的脚本将捕获所有 URL 类型并将它们转换为可点击的链接.

                  The script below will catch all URL types and convert them to clickable links.

                  $url = '@(http)?(s)?(://)?(([a-zA-Z])([-w]+.)+([^s.]+[^s]*)+[^,.s])@';
                  $string = preg_replace($url, '<a href="http$2://$4" target="_blank" title="$0">$0</a>', $string);
                  echo $string;
                  

                  新的更新,如果你有字符串去掉 (s) 然后使用下面的代码块,感谢@AndrewEllis 指出这一点.

                  The new update, If you're having the string strip the (s) then use the below code block, Thanks to @AndrewEllis for pointing this out.

                  $url = '@(http(s)?)?(://)?(([a-zA-Z])([-w]+.)+([^s.]+[^s]*)+[^,.s])@';
                  $string = preg_replace($url, '<a href="http$2://$4" target="_blank" title="$0">$0</a>', $string);
                  echo $string;
                  

                  这里有一个非常简单的解决 URL 显示不正确的方法.

                  Here's a very simple solution for the URL not displaying correctly.

                  $email = '<a href="mailto:email@email.com">email@email.com</a>';
                  $string = $email;
                  echo $string;
                  

                  这是一个非常简单的修复,但您必须根据自己的目的对其进行修改.

                  It is a very simple fix but you will have to modify it for your own purpose.

                  我提供了多个答案,因为有些服务器的设置不同,所以一个答案可能适用于某些人,但不适用于其他人,但我希望答案对您有用,如果没有,请告诉我,希望,我可以想出另一个解决方案.

                  I've provided multiple answers as some servers are set up differently, so one answer may work for some but not for others, but I hope the answer(s) work for you and if not then let me know, and hopefully, I can come up with another solution.

                  有多个脚本,因为有些 PHP 文件需要不同的脚本,有些服务器的设置也不同,而且每个都有不同的要求,有些只需要 HTTP/S,有些需要 WWW,有些需要 FTP/S,每个都可以工作关于如何设置用户自己的脚本,我为每个人提供了一些文字说明他们的工作.

                  There are multiple scripts as some PHP files require different scripts also some servers are set up differently, Plus each has different requirements, Some want just HTTP/S, some want WWW and some want FTP/S, Each one will work depending on how the users own scripts are set up, I provided some text with each one with what they do.

                  这篇关于在 PHP 中将纯文本 URL 转换为 HTML 超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                    <tbody id='uuL75'></tbody>
                • <tfoot id='uuL75'></tfoot>
                    • <legend id='uuL75'><style id='uuL75'><dir id='uuL75'><q id='uuL75'></q></dir></style></legend>

                      <small id='uuL75'></small><noframes id='uuL75'>

                          <bdo id='uuL75'></bdo><ul id='uuL75'></ul>
                        • <i id='uuL75'><tr id='uuL75'><dt id='uuL75'><q id='uuL75'><span id='uuL75'><b id='uuL75'><form id='uuL75'><ins id='uuL75'></ins><ul id='uuL75'></ul><sub id='uuL75'></sub></form><legend id='uuL75'></legend><bdo id='uuL75'><pre id='uuL75'><center id='uuL75'></center></pre></bdo></b><th id='uuL75'></th></span></q></dt></tr></i><div id='uuL75'><tfoot id='uuL75'></tfoot><dl id='uuL75'><fieldset id='uuL75'></fieldset></dl></div>