0

Hi Friends i have this problem.

I have this

<div  id="text">
Hello world this just a demo.
http://upload.wikimedia.org/wikipedia/it/e/e1/Nba1.png
</div>

Now i need to convert that in this with jquery.

<div id="text">
Hello world this just a demo.
<img src="http://upload.wikimedia.org/wikipedia/it/e/e1/Nba1.png" width="150" height="150" alt="Nba">
</div>

Some ideas or already plugin. 1 Step ist detecting a image url form div id text 2 Replace the detected image url with html content 3 if detected url if ist not a image replace them with html content Thank You

Robert Mrsic
  • 53
  • 2
  • 7

3 Answers3

0

This is how you do it in PHP, and I don't see a reason for not doing it in PHP

<?php

function get_string_between($string, $start, $end){
    $string = " ".$string;
    $ini = strpos($string,$start);
    if ($ini == 0) return "";
    $ini += strlen($start);
    $len = strpos($string,$end,$ini) - $ini;
    return substr($string,$ini,$len);
}

$fullstring = '
 <div  id="text">
 Hello world this just a demo.
 http://upload.wikimedia.org/wikipedia/it/e/e1/Nba1.png
 </div>';

$start = "http:";
$end = "</div>";

$parsed = get_string_between($fullstring, $start, $end);

echo $start.$parsed; // (result = dog)

?>

and I think the extension check is pretty straight forward, just check google "PHP check valid image extension" or similar query.

Grigor
  • 4,139
  • 10
  • 41
  • 79
0

First add a id to your url

  <div  id="text">
  Hello world this just a demo.
  <div id="img">http://upload.wikimedia.org/wikipedia/it/e/e1/Nba1.png</div>
  </div>

Then get url to jquery like this

var url=$('#img').text();

Then add image tags to it and append:

var real_img='<img src="'+url+'" width="150" height="150" alt="Nba">';

$('#img').html(real_img);
Jadzia
  • 164
  • 1
  • 9
0

With Help of @Engineer here ist the solution for sombody that have same problem as me

HTML

<div id="content-url">
    Hello World<br>
 http://www.goalterest.com/  
    http://www.jquery.com/  
    http://www.google.com/  
 http://www.esotech.org/wp-content/uploads/2011/12/jquery_logo.png
    http://www.linuxtrent.it/sites/default/files/images/drupal-logo.jpg
    <div id="urls"></div>
    <div id="photos"></div>
</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);

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

$.each( url_url, function(i,value){
   var convert_url='<a href="'+url_url[i]+'">'+url_url[i]+'</a><br>';

   $('#urls').append(convert_url)
       });
    $.each( url_photo, function(i,value){

   var convert_photo='<img src="'+url_photo[i]+'" width="150" height="150" alt="Nba"><br>';
   $('#photos').append(convert_photo)
});

Thanks to ALL

Robert Mrsic
  • 53
  • 2
  • 7