0

i have this variable $img = $_POST['cur_image'];

where the content of cur_image and so $img is:

<img style="display: inline;" src="http://news.bbcimg.co.uk/media/images/65348000/jpg/_65348094_belreuters.jpg" id="1" width="100"><img style="display: none;" src="http://news.bbcimg.co.uk/media/images/65356000/jpg/_65356067_65356066.jpg" id="2" width="100"><img style="display: none;" src="http://news.bbcimg.co.uk/media/images/65367000/jpg/_65367308_coffee.jpg" id="3" width="100"><img style="display: none;" src="http://news.bbcimg.co.uk/media/images/65376000/jpg/_65376729_heart.jpg" id="4" width="100">

So i need to extract only link of the first image that i got i.e. (http://news.bbcimg.co.uk/media/images/65348000/jpg/_65348094_belreuters.jpg)

and than send to db such as

mysql_query("INSERT INTO `table` (img) VALUES ('$img')");
Fabio Krueger
  • 65
  • 1
  • 8

2 Answers2

0

How about something like this? (untested)

$tmp_img = $_POST['cur_image'];

$img_path_offset = strpos($tmp_img, "src=") + 4;
$img_path_end = strpos($tmp_img, "\"", $img_path_offset);

$img = substr($tmp_img, $img_path_offset, $img_path_length);

And then run your MySql..

Cheers, Viggo

ViggoV
  • 2,133
  • 2
  • 20
  • 22
  • Alternatively have a look here: http://stackoverflow.com/questions/3627489/php-parse-html-code – ViggoV Jan 21 '13 at 14:47
  • yes sure" only problem is that's ==>"http://graphics8.nytimes.com/image...there is a " before link and don't get until .jpg bu for the rest si correct...how can i adjust this ?? – Fabio Krueger Jan 21 '13 at 15:00
  • Oh sorry, add 5 instead of 4 to exclude the first ": $img_path_offset = strpos($tmp_img, "src=") + 5; You might have to tweak the numbers to get the exact string you need.. – ViggoV Jan 21 '13 at 15:10
  • nice..now result is http://graphics8.nytimes.com/images/2013/01/18/world/africa/Algeria/Algeria-articleInline.jpg" id="1" width="100"> – Fabio Krueger Jan 21 '13 at 15:17
  • Arh, sorry again :) you need to subtract the offset so the last line becomes: "$img = substr($tmp_img, $img_path_offset, $img_path_end-$img_path_offset)" – ViggoV Jan 21 '13 at 16:16
  • I recommend you hit up the functions on http://php.net/manual/en/ to gain a better understanding of what is going on.. – ViggoV Jan 21 '13 at 16:18
  • sure i will see, thank you so much for your time...now working good – Fabio Krueger Jan 21 '13 at 16:26
0

Using preg_match you could do something like:

$tmp_img = $_POST['cur_image'];
$match = "src=[\"'](.+?)[\"']"
preg_match($match, $tmp_img, $matches_array);
$img = substr($matches_array[0], 4, -1);

And then your MySql..

ViggoV
  • 2,133
  • 2
  • 20
  • 22