4

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.

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

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...

For the YouTube-thingy I tried this without success:

if (preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $message, $m)) {
    $video_id = $m[1];
    $message = preg_replace("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#","<iframe class='embedded-video' src='http://www.youtube.com/embed/" . $video_id . "' allowfullscreen></iframe>",$message);
}
2
  • Please give examples of URLs you want to match Commented Nov 6, 2011 at 12:40
  • URLs ending with .jpg || .gif || .png sohuld be embedded images. youtube videos (youtube.com/watch?v=XnWyrdBX08w&feature=feedu) should extract to video id (in this case XnWyrdBX08w) and put it like described above in an iframe. All other URLs should be clickable links (<a href=""></a>) Commented Nov 6, 2011 at 12:45

3 Answers 3

14

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice =) Gotta love regex callbacks.
2

I had some issues with the preg_replace_callback when the text included three dots .... The above code recognized the three dots as a URL which is not true.

Here is my fix and it seems to work at the moment $str = preg_replace_callback('#(?:https?://\S+)|(?:www.\S+)|(?:jpe?g|png|gif)#', function($arr)

Would this fix fail in other cases???

Comments

0

The code of Tim Cooper doesn't working with https link. Ex: https://www.facebook.com/ It will return http://https://www.facebook.com/

Replace

if(strpos($arr[0], 'http://') !== 0)

by

if(strpos($arr[0], 'http') !== 0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.