1

I have this string:

<img src="http://localhost:8080/omeka3/files/square_thumbnails/a1641b89b518599b049efa6017f92040.jpg" alt="altText" title="Title">

I want to extract the value of the src attribute, to create a meta element like this:

<meta itemprop='thumbnailUrl' content='http://localhost:8080/omeka3/files/square_thumbnails/a1641b89b518599b049efa6017f92040.jpg' />

I tried the explode function:

$img_tag = item_image('square_thumbnail');
$thumbnailUrl = explode("=",$img_tag);
echo "<meta itemprop='thumbnailUrl' content='".$thumbnailUrl[1]."'/>";

But the result is:

<meta itemprop='thumbnailUrl' content='"http://localhost:8080/omeka3/files/square_thumbnails/a1641b89b518599b049efa6017f92040.jpg" alt'/>

There's any way to indicate the limit of the string at .jpg, with explode() or another function?

Thanks!

1 Answers1

1

You could use DOMDocument class (available in PHP 5 and higher) to parse the src attribute of your <img> HTML tag:

    $input = '<img src="http://localhost:8080/omeka3/files/square_thumbnails/a1641b89b518599b049efa6017f92040.jpg" alt="altText" title="Title">';

    $dom = new DOMDocument();
    $dom->loadHTML($input);
    $nodes = $dom->getElementsByTagName('img');

    if ($nodes->length == 1) {
        echo $nodes->item(0)->getAttribute('src');
    }
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Denis Mysenko
  • 6,366
  • 1
  • 24
  • 33