0

Possible Duplicate:
How to extract img src, title and alt from html using php?

This would be an example of string:

$src = '<img src="/avatars/admin.jpg" class="avatar 23 avatar-30 avatar-default" height="30" width="30" style="width: 30px; height: 30px;" alt="avatar">';

$out should be /avatars/admin.jpg

Problem is that /avatars/admin.jpg is path to avatar of current logged user, so it's dynamically changed.

How to get $out in simplest way?

Community
  • 1
  • 1
enloz
  • 5,704
  • 9
  • 37
  • 56
  • I'm PHP noob, I have only idea how to do this with jQuery .attr() , but that is bad solution. I would be thankful just for guideline, place to start. – enloz Mar 12 '12 at 13:54
  • @enloz you can do as mentioned by alex. The same question was asked before [http://stackoverflow.com/questions/138313/how-to-extract-img-src-title-and-alt-from-html-using-php](http://stackoverflow.com/questions/138313/how-to-extract-img-src-title-and-alt-from-html-using-php) – Rajan Rawal Mar 12 '12 at 14:00

6 Answers6

1

Well, if this is the only HTML you need to process, you could probably get away with a regex...

preg_match('/\ssrc="([^"]+)"/', $src, $matches);
alex
  • 479,566
  • 201
  • 878
  • 984
0

look at http://php.net/manual/de/function.substr.php and http://php.net/manual/de/function.strpos.php

you could use a combination of both, such as substr( $string, strpos('src="') + 5, strpos('.jpg') + 4 );

i didnt try it, cuz i dont really know what u r trying to "extract" , but now u get the basic idea ;)

g.r.

Ace
  • 1,437
  • 6
  • 28
  • 46
0

Any chance you can set the string to be:

$str = '<img src="/avatars/$avatar.jpg" class="avatar 23 avatar-30 avatar-default" height="30" width="30" style="width: 30px; height: 30px;" alt="avatar">';

Otherwise, I'd use str_replace

Jakob Jingleheimer
  • 30,952
  • 27
  • 76
  • 126
0

use this

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 = "$src = '<img src="/avatars/admin.jpg" class="avatar 23 avatar-30 avatar-default" height="30" width="30" style="width: 30px; height: 30px;" alt="avatar">';
";
$parsed = get_string_between($fullstring, "<img src=", " class=");

echo $parsed; //

thanks

Saiyam Patel
  • 1,161
  • 9
  • 18
0

Try something like this.

<?php
$src = '<img src="/avatars/admin.jpg" class="avatar 23 avatar-30 avatar-default" height="30" width="30" style="width: 30px; height: 30px;" alt="avatar">';
$regex = '|src="(.+?)"|';
preg_match($regex,$src,$match);
$out = $match[1];
?>
Stanley
  • 3,935
  • 2
  • 18
  • 22
0

Just using substr and strpos

$src = '<img src="/avatars/admin.jpg" class="avatar 23 avatar-30 avatar-default" \
height="30" width="30" style="width: 30px; height: 30px;" alt="avatar">';

$start = strpos($src, '"') + 1;
$end = $start + strpos($src, '"', $start - 1) - 1;
echo substr($src, $start, $end);
Jonathan
  • 10,936
  • 8
  • 64
  • 79