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

    <bdo id='PBWN8'></bdo><ul id='PBWN8'></ul>

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

    <legend id='PBWN8'><style id='PBWN8'><dir id='PBWN8'><q id='PBWN8'></q></dir></style></legend>
    <tfoot id='PBWN8'></tfoot>

    1. Regex PHP - 自动检测 YouTube、图像和“常规"链接

      Regex PHP - Auto-detect YouTube, image and quot;regularquot; links(Regex PHP - 自动检测 YouTube、图像和“常规链接)
        <bdo id='tbyES'></bdo><ul id='tbyES'></ul>
        1. <i id='tbyES'><tr id='tbyES'><dt id='tbyES'><q id='tbyES'><span id='tbyES'><b id='tbyES'><form id='tbyES'><ins id='tbyES'></ins><ul id='tbyES'></ul><sub id='tbyES'></sub></form><legend id='tbyES'></legend><bdo id='tbyES'><pre id='tbyES'><center id='tbyES'></center></pre></bdo></b><th id='tbyES'></th></span></q></dt></tr></i><div id='tbyES'><tfoot id='tbyES'></tfoot><dl id='tbyES'><fieldset id='tbyES'></fieldset></dl></div>
            <tbody id='tbyES'></tbody>

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

            • <legend id='tbyES'><style id='tbyES'><dir id='tbyES'><q id='tbyES'></q></dir></style></legend>
                <tfoot id='tbyES'></tfoot>

              1. 本文介绍了Regex PHP - 自动检测 YouTube、图像和“常规"链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我希望在我的聊天应用程序中,可以点击指向网站的链接,并自动嵌入指向 YouTube 和图像的链接.

                I want to make it so that in my chat-application, links to websites can be clickable and links to YouTube and images automatically gets embedded.

                我已经为我的 WebIRC 客户端使用 Java 编写了这段代码,但现在我正在尝试使用 PHP 和 JavaScript 编写它.

                I've made this code in Java for my WebIRC client but now I'm trying to make it in PHP and JavaScript.

                我还不熟悉 PHP,所以我不太了解在那里使用正则表达式.我想知道是否有好心人能帮我解决这个问题...

                I'm not familiar with PHP yet so I don't know that much about using regex there. I wonder if some kind soul could help me with this...

                对于 YouTube 的东西,我尝试过但没有成功:

                For the YouTube-thingy I tried this without success:

                if (preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v/)[^&
                ]+|(?<=v=)[^&
                ]+|(?<=youtu.be/)[^&
                ]+#", $message, $m)) {
                    $video_id = $m[1];
                    $message = preg_replace("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v/)[^&
                ]+|(?<=v=)[^&
                ]+|(?<=youtu.be/)[^&
                ]+#","<iframe class='embedded-video' src='http://www.youtube.com/embed/" . $video_id . "' allowfullscreen></iframe>",$message);
                }
                

                推荐答案

                这是我想出的解决方案:

                Here is a solution I came up with:

                $str = 'This is an image: google.ca/images/srpr/logo3w.png
                
                YouTube: http://www.youtube.com/watch?v=V2b8ilapFrI&feature=related
                
                Stackoverflow:  http://stackoverflow.com/';
                
                $str = preg_replace_callback('#(?:https?://S+)|(?:www.S+)|(?:S+.S+)#', function($arr)
                {
                    if(strpos($arr[0], 'http://') !== 0)
                    {
                        $arr[0] = 'http://' . $arr[0];
                    }
                    $url = parse_url($arr[0]);
                
                    // images
                    if(preg_match('#.(png|jpg|gif)$#', $url['path']))
                    {
                        return '<img src="'. $arr[0] . '" />';
                    }
                    // youtube
                    if(in_array($url['host'], array('www.youtube.com', 'youtube.com'))
                      && $url['path'] == '/watch'
                      && isset($url['query']))
                    {
                        parse_str($url['query'], $query);
                        return sprintf('<iframe class="embedded-video" src="http://www.youtube.com/embed/%s" allowfullscreen></iframe>', $query['v']);
                    }
                    //links
                    return sprintf('<a href="%1$s">%1$s</a>', $arr[0]);
                }, $str);
                

                如果您需要我为您澄清任何事情,请告诉我.

                Let me know if you need me to clarify anything for you.

                这篇关于Regex PHP - 自动检测 YouTube、图像和“常规"链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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 的问题)

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

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

                        1. <legend id='OJdKw'><style id='OJdKw'><dir id='OJdKw'><q id='OJdKw'></q></dir></style></legend>