0

I get this stored in a PHP variable as a string:

$article-text = '<p><img src="img/1.jpg" class="article-image">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p><p><img src="img/2.jpg" class="article-image">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>';

I need to get the src of first img, any help?

911madza
  • 80
  • 8

1 Answers1

1

With help of a regex this is simple.

$articletext = '<p><img src="img/1.jpg" class="article-image">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p><p><img src="img/2.jpg" class="article-image">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>';

$matches = [];
$src = '';

if(preg_match('/src="(.*?)"/', $articletext, $matches)) {
    $src = $matches[1];
}

echo $src;

img/1.jpg

By the way, variables can not include a dash.

Markus Zeller
  • 8,516
  • 2
  • 29
  • 35