Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Detect and Convert URL images and Videos from Plain Text URL's?

Hi i need to detect and convert urls images and video from plain text urls

Html

<div id="content-url">
 Hello World<br>
 http://www.goalterest.com/  <br>
 http://www.esotech.org/wp-content/uploads/2011/12/jquery_logo.png
 http://www.esotech.org/wp-content/uploads
</div>

Jquery

var urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
var photoRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]).(?:jpg|gif|png)/ig;

  var url_url= $('#content-url').html().match(urlRegex);
var url_photo= $('#content-url').html().match(photoRegex);

  var convert_url='<a href="'+url_url+'">'+url_url+'</a>';
var convert_photo='<img src="'+url_photo+'" width="150" height="150" alt="Nba">';
$('#content-url').append(convert_url);
$('#content-url').append(convert_photo);

Here Demo http://jsfiddle.net/nqVJc/3/

In the demo i get the way to detect and convert to url and photo but the problem ist when ist there multiple urls The Urls are not separated

like image 550
Robert Mrsic Avatar asked Jan 23 '26 13:01

Robert Mrsic


1 Answers

You are not iterating through the matched elements :

$.each( url_url, function(i,value){
   var convert_url='<a href="'+url_url[i]+'">'+url_url[i]+'</a>';
   var convert_photo='<img src="'+url_photo[i]+'" width="150" height="150" alt="Nba">';
   $('#content-url').append(convert_url,convert_photo)
});

Here is DEMO.

For removing matched urls you need to add this lines before $.each :

$('#content-url').html( $('#content-url').html().replace(urlRegex,''));

//$.each goes here
like image 118
Engineer Avatar answered Jan 25 '26 02:01

Engineer