hi guys,

i have a rege code that looks to see if a block of text has a link.
i have modded it to show the link as an image if it exists (badly i know this method isnt very good)

what i need to do is check if the text contains a link, image,or video (youtube/dailymotion etc) and then display the right output

heres what i have

<?php 
        $convertlinkmsg = $message->message;
        $convertlinkmsg  = preg_replace( '/(http|ftp)+(s)?:(\/\/)((\w|\.)+)(\/)?(\S+)?/i', '<a target="_blank" class="imgsoc "  href="\0"><img src="\0" alt="\4"/></a>', $convertlinkmsg); 

        echo $convertlinkmsg ; ?> 

any ideas?
thanks

Member Avatar for diafol

How do you want to replace images and videos? With the actual image or a video iframe?
Perhaps an easier way would be to search for .png, .jp(e)g, .gif, .svg files and replace those with an img tag BUT some online images are sometimes generated via php files (e.g. image.php?id=3456), so it's not always as straightforward.

For the videos, those links may have 'youtu(.)be' etc in them, but equally other non-youtube urls may have a 'youtube' folder - so you'll have to be very specific.

I assume you'll need a different regex for each video site.

Instead of listing loads of separate preg_replace() you can use arrays for the 'search' and 'replace' parameters.

JS idea: http://stackoverflow.com/questions/7168987/how-to-convert-a-youtube-video-url-to-the-iframe-embed-code-using-jquery-on-pag

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.