I have this kind of string
$string='Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type <img src="http://www.yourwebite.com/images/assets/livechat-off.png" alt="Our products"/> and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<img src="http://www.yourwebite.com/images/youareon.jpg" alt="Our products"/>the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic';
What i want is to take first img from $string and to get new string like this
$newstring='<img src="http://www.yourwebite.com/images/assets/livechat-off.png" alt="Our products"/>';
That way i can take just one picture from string and display it anywhere, is taht possible?
Tried strip_tags($string, '<img>');
but that way i got all IMG tags, i just want one from string, maybe random, does not to be the first :)
Just made function with help of PeeHaa, txanks mate :)
public function getimagefromstring($string)
{
$dom = new DOMDocument();
// load the string as an HTML document
$dom->loadHTML($string);
$xpath = new DOMXPath($dom);
// match the first image tag found in the document
$node = $xpath->query('//img[1]');
// no images found
if (!$node->length) {
$image='Hello World';
}
else
{
// build the image attributes
$attrs = '';
if ($node->item(0)->hasAttributes()) {
foreach ($node->item(0)->attributes as $attribute) {
$attrs.= ' ' . $attribute->name . '="' . $attribute->value . '"';
}
}
// display the image
$image='<img' . $attrs . '>';
}
return $image;
}